Your YourModelForm is a modelform of a model.  If you're setting a
property of that model, AND YOU WANT IT TO PERSIST IN THE DATABASE,
you set the property equal to the request.user and save that object.
If you don't have a foreignkeyfield to User, there's no way you're
going to associate that model with a user and have it persist.
Setting it in memory is going to be useless (unless you use it in the
view/template immediately after you associate the user with the
model).

class YourModel(inherit from whatever the model is that you should
inherit from):
    field = models.Charfield(...blahblahblah)
    user = models.ForeignKeywhatever()

Then do what Thorsten said.

On Feb 21, 2:28 pm, ds39 <sdavid...@gmail.com> wrote:
> I just have one final question. The method suggested by Thorsten
> appears to work. But, is the attached user object an accessible filter
> parameter in the API now ? For instance, I don't see it listed as a
> part of the actual model in the API. How would I go about accessing
> it ?
>
> On Feb 20, 5:25 pm, Thorsten Sanders <thorsten.sand...@gmx.net> wrote:
>
>
>
>
>
>
>
> > You could do for example:
>
> > exclude the user field from the form and in your view something like this:
>
> > form = YourModelForm(request.POST)  #fill the modelform with the data
> >          if form.is_valid(): # check if valid
> >              mynewobject = form.save(commit=False) #save it to create
> > the object but dont send to database
> >              mynewobject.user = request.user # attach the user to it
> >              mynewobject.save() # now do the real save and send it to
> > the database
>
> > Am 20.02.2012 22:59, schrieb ds39:
>
> > > I hate to keep bringing this issue up, but I'm still not entirely sure
> > > how to implement this. I've tried a number of different ways to
> > > connect some kind of user ID with form data without much success. Is
> > > the idea that after authenticating the user in the view, request.user
> > > be set to some variable that allows the user ID to be added to the
> > > model or ModelForm ? Would this make the user object associated with
> > > the form or model object accessible by filtering in the API ?
>
> > > Thanks again
>
> > > On Feb 19, 9:48 pm, Shawn Milochik<sh...@milochik.com>  wrote:
> > >> On 02/19/2012 09:29 PM, ds39 wrote:
>
> > >>> Thanks for your response. But, would you mind expanding on it a little
> > >>> bit ?
> > >> How about you give it a try and see what you can figure out? In your
> > >> view, request.user will return the currently logged-in user (or an
> > >> AnonymousUser if they're not logged in). Since you said your view
> > >> requires login, you'll have a User object all ready to go.
>
> > >> Shawn

-- 
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.

Reply via email to