In one of my Django models, I override the save function. I do this to get the user’s username. But that keeps failing.
this is what i did:
def save(self, *args, **kwargs):
self.slug = slugify('%s' % (self.question))
if not self.id:
self.publish_date = datetime.datetime.now()
self.publisher = self.request.user
self.modification_date = datetime.datetime.now()
self.modifier = self.request.user
super(Faq, self).save(*args, **kwargs) # Call the "real" save() method
This fails with:
‘Faq’ object has no attribute ‘request’
Thanks.
If this is for use within the Admin app, as you say in your answer to Jake, then you shouldn’t override the model save method at all. Instead, you should override the
save_modelmethod of theModelAdminclass.See the original code in
django.contrib.admin.options– you’ll see that it’s already passed therequestobject. So all you need to do is to assign thepublisherthere: