Re: where is 'request' as in from x import y
The request object is passed to the views in your views.py you have mapped to in your urls.py Do you understand the layout of a basic django project? A python module is not executed to handle a request, but is imported at the startup of the process and your view functions are called depending on the URL. On Thu, May 26, 2011 at 7:17 AM, MikeKJ wrote: > > I need to get the auth user id in a save method on a model so > > from django.contrib.auth.models import User > user_id = request.user.id > > > the error comes back as > > name 'request' is not defined > > Cheers > -- > View this message in context: > http://old.nabble.com/where-is-%27request%27-as-in-from-x-import-y-tp31706142p31706142.html > Sent from the django-users mailing list archive at Nabble.com. > > -- > 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. > > -- Read my blog! I depend on your acceptance of my opinion! I am interesting! http://techblog.ironfroggy.com/ Follow me if you're into that sort of thing: http://www.twitter.com/ironfroggy -- 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: where is 'request' as in from x import y
Is this really only available to a view? All I want is to know is the auth user id currently logged in so I can save it into another model, has to be in the model so it is part of the admin save method MikeKJ wrote: > > I need to get the auth user id in a save method on a model so > > from django.contrib.auth.models import User > user_id = request.user.id > > > the error comes back as > > name 'request' is not defined > > Cheers > -- View this message in context: http://old.nabble.com/where-is-%27request%27-as-in-from-x-import-y-tp31706142p31706197.html Sent from the django-users mailing list archive at Nabble.com. -- 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: where is 'request' as in from x import y
On Thursday, May 26, 2011 12:52:28 PM UTC+1, MikeKJ wrote: > > > Is this really only available to a view? All I want is to know is the auth > user id currently logged in so I can save it into another model, has to be > in the model so it is part of the admin save method You can't access something in a method unless it gets passed there, of course. "request" isn't some static object, it's instantiated at the beginning of every actual request, naturally. So you can't just import it - what would you import? Now, your last sentence reveals what you actually want to do, and of course Django has a way to achieve that: override the `save_model` method in the ModelAdmin class. That method does get the request passed in. -- 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.
Re: where is 'request' as in from x import y
Interesting, I thought I was doing that in my save method but obviously I cant be. example. Model 1: auth_user_id row_id_of model_being_saved timestamp Model 2: something def save(self): auth_user_id = request.user.id row_id_of_model_being_saved = something Model 1.save() super( Model 2, self).save() I should not that this is retro stuff as in 0.97 pre code (old site), now that throws the request error Cheers Now, your last sentence reveals what you actually want to do, and of course Django has a way to achieve that: override the `save_model` method in the ModelAdmin class. That method does get the request passed in. -- DR. -- -- View this message in context: http://old.nabble.com/where-is-%27request%27-as-in-from-x-import-y-tp31706142p31714838.html Sent from the django-users mailing list archive at Nabble.com. -- 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: where is 'request' as in from x import y
On May 27, 10:58 am, MikeKJ wrote: > Interesting, I thought I was doing that in my save method but obviously I > cant be. Of course it can't. The request only exists when there's an actual HTTP request sent to your HTTP server. Your model code is (hopefully) totally independant and unaware of the mere existence of something like an HTTP request. How else would you work with your data outside HTTP requests ? (I mean management scripts / commands, cron jobs, GUI or CLI client, whatever). -- 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.