I’m trying to override the save() method in the admin so when my publisher-users are creating their customer-users, the publisher field is automatically assigned a value of current user.
ValueError: Cannot assign "User: foo": "SimpleSubscriber.publisher" must be a "Publisher" instance.
I used this tutorial to get started: here.
def save_model(self, request, obj, form, change):
if not change:
obj.publisher = request.user
obj.save()
this is the save method override. The only users who can access the admin are publishers, and both the Publisher and SimpleSubscriber models are user models:
class Publisher(User):
def __unicode__(self):
return self.get_full_name()
class SimpleSubscriber(User):
publisher = models.ForeignKey(Publisher)
address = models.CharField(max_length=200)
city = models.CharField(max_length=100)
state = USStateField()
zipcode = models.CharField(max_length=9)
phone = models.CharField(max_length=10)
date_created = models.DateField(null=True)
sub_type = models.ForeignKey(Product)
sub_startdate = models.DateField()
def __unicode__(self):
return self.last_name
What can I replace request.user with in order to assign each new SimpleSubscriber to the current publisher user?
You must replace
request.userwith an instance ofPublisher.One way to do that would be:
Of course, that will do a lookup every time you invoke it, so you might like to design your application to do this on every request.
Another way to do this is to (slightly) abuse the django model system – where one model inherits from another, the corresponding parent and child model instances have the same
id(by default);