Re: Associating Form Data with Users

2012-02-21 Thread Kelly Nicholes
And by "API" do you mean "ORM?"

On Feb 21, 10:45 pm, Kelly Nicholes  wrote:
> errr--
>
> django.contrib.auth.models import User
>
> class YourModel(models.Model):
>     user = models.ForeignKey(User)
>
> On Feb 21, 10:42 pm, Kelly Nicholes  wrote:
>
>
>
>
>
>
>
> > 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  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  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  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.



Re: Associating Form Data with Users

2012-02-21 Thread Kelly Nicholes
errr--

django.contrib.auth.models import User

class YourModel(models.Model):
user = models.ForeignKey(User)

On Feb 21, 10:42 pm, Kelly Nicholes  wrote:
> 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  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  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  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.



Re: Associating Form Data with Users

2012-02-21 Thread Kelly Nicholes
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  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  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  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.



Re: Associating Form Data with Users

2012-02-21 Thread ds39
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  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  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.



Re: Associating Form Data with Users

2012-02-20 Thread Thorsten Sanders

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



Re: Associating Form Data with Users

2012-02-20 Thread 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  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.



Re: Associating Form Data with Users

2012-02-19 Thread Shawn Milochik

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.



Re: Associating Form Data with Users

2012-02-19 Thread ds39
Thanks for your response. But, would you mind expanding on it a little
bit ?

On Feb 19, 9:21 pm, Shawn Milochik  wrote:
> When you process the form in your view, you'll have access to
> request.user. Just use that.

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



Re: Associating Form Data with Users

2012-02-19 Thread Shawn Milochik
When you process the form in your view, you'll have access to 
request.user. Just use that.


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



Associating Form Data with Users

2012-02-19 Thread ds39
Hi All,

I was wondering if someone could tell me the correct method for
associating submitted form data with a user. I would like to access
user-submitted form information with the API (or a template) via user
identification. I'm making use of a ModelForm in a forms.py file which
corresponds to a "Suggestions" model in my models.py file. In my view,
I've specified that the user must be authenticated in order to access
and submit form data. But, I'm still not sure how to associate the
data with the particular authenticated user. The methods I've tried so
far create a drop-down list where the user can manually specify their
name before submitting the form. But, I would like the user to be
associated with the data automatically upon submission. Some of the
tutorials appear to talk about this issue in terms of extending the
user profile, but I think I'm looking for something different. Could
anyone provide a basic explanation or point me to a page that does ? I
hope I've described the situation clearly enough.

Thanks

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