On Tuesday, February 15, 2011 8:05:31 PM UTC, jonas wrote: > > Thanks for your reply. But I found out about save_model(). > From there I have access to the HttpRequest object that contains my > contrib.auth.models.User model. > > But that seemed to be half the story. > > class Post(models.Model): > def save_model(self, req, obj, form, change): > if self.author is None: > self.author = req.User > > Doesn't seem to do the trick. I'm getting error reports telling me > author_id can't be null. > And indeed if I look in my mysql database I see a author_id column and no > author column. Makes sence since author is of the type models.ForeignKey(). > > Anyway how do I get around this ? > > Regards, > > Jonas. >
It doesn't work because that code isn't getting run at all, as you'll see if you put debugging into it. save_model is an admin method, on the ModelAdmin class. It's not a method on the model class, and won't work outside of the admin. Tom has already given you the correct solution. -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.