On Thursday, March 17, 2011 2:33:50 PM UTC, AJ wrote:
>
> No one? 
>
> On Mar 16, 10:05 pm, AJ <[email protected]> wrote: 
> > I have a model like this: 
> > 
> > class Post (models.Model): 
> >     name = models.CharField(max_length=1000, help_text="required, name 
> > of the post") 
> >     description = models.TextField(blank=True) 
> >     custom_hashed_url = models.CharField(unique=True, max_length=1000, 
> > editable=False) 
> > 
> > def save(self, *args, **kwargs): 
> >         #How to save User here? 
> >         super(Model, self).save() 
> > 
> > View code: 
> > 
> > if not errors: 
> >     f = PostForm(request.POST) 
> >     f.save() 
> > 
> > There is an old post that I followed but could not do it. And also 
> > this post is old.
> http://www.b-list.org/weblog/2006/nov/02/django-tips-auto-populated-f... 
> > 
> > Even though I have the user field as FK, I get this error: 'Cannot 
> > assign None: "MyModel.user" does not allow null values.' 
> > 
> > This essentially means (IMHO) that my View does not send the User 
> > along. How can I auto populate the user field with Django User, the 
> > currently logged in user.


Don't be rude - it's only a few hours since your initial query. Perhaps all 
those who knew the answer were busy, or haven't looked at the list since you 
posted.

Anyway, the answer is that you don't do this in the model. You do ti in the 
view:

    form = PostForm(request.POST)
    if form.is_valid():
        post = form.save(commit=False)
        post.user = request.user
        post.save()

Of course, your posted model code doesn't actually include a FK to User - I 
assume you have one, because otherwise you wouldn't have got that error. It 
would have been helpful to post the actual code. 
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to