"request"object within models.py

93 views
Skip to first unread message

enri57ar

unread,
May 19, 2008, 4:24:41 PM5/19/08
to Django users
How access to request object within models ?


from django.contrib.auth.models import User

Class Message(models.Model):
message = ...
user_id = models.ForeignKey(User)

def save(self):
user_id = request.user.id # doesn't work
super(Mensaje, self).save()

Richard Dahl

unread,
May 19, 2008, 4:35:49 PM5/19/08
to django...@googlegroups.com

James Bennett

unread,
May 19, 2008, 4:38:08 PM5/19/08
to django...@googlegroups.com
On Mon, May 19, 2008 at 3:24 PM, enri57ar <enri...@gmail.com> wrote:
> How access to request object within models ?

Pass it as an argument the same as any other value. Magical hacks to
try to make it available otherwise are likely to land you in trouble
later on.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Richard Dahl

unread,
May 19, 2008, 4:45:14 PM5/19/08
to django...@googlegroups.com
how do you pass the request object to models?
-richard

James Bennett

unread,
May 19, 2008, 4:52:24 PM5/19/08
to django...@googlegroups.com
On Mon, May 19, 2008 at 3:45 PM, Richard Dahl <ric...@dahl.us> wrote:
> how do you pass the request object to models?

Same way you pass any argument to any function or method in Python:
write your function/method to accept the argument, and pass it from
the code that calls the function/method.

enri57ar

unread,
May 19, 2008, 5:09:52 PM5/19/08
to Django users
On May 19, 5:35 pm, "Richard Dahl" <rich...@dahl.us> wrote:
> http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
> -richard
>

Thanks!, it works

Tim Chase

unread,
May 19, 2008, 5:10:06 PM5/19/08
to django...@googlegroups.com
>> how do you pass the request object to models?
>
> Same way you pass any argument to any function or method in
> Python: write your function/method to accept the argument, and
> pass it from the code that calls the function/method.

It's the last bit that can throw folks...many folks seem to use
very nice/helpful bits of the framework that abstract the save()
call so it's never thought-about. Wouldn't adding a parameter to
save() stymie the admin and a number of other places that call
save() without parameters?

I guess that's part of the price to pay for including user info
in a model? :-/

-tim

James Bennett

unread,
May 19, 2008, 5:17:48 PM5/19/08
to django...@googlegroups.com
On Mon, May 19, 2008 at 4:10 PM, Tim Chase
<django...@tim.thechases.com> wrote:
> It's the last bit that can throw folks...many folks seem to use
> very nice/helpful bits of the framework that abstract the save()
> call so it's never thought-about. Wouldn't adding a parameter to
> save() stymie the admin and a number of other places that call
> save() without parameters?

Well, for years now I've been telling anyone who will listen that
auto-filling a foreign key in the admin like this is generally a bad
idea; if you don't trust your site administrators to fill out the form
properly, you've got much bigger problems. Plus, newforms-admin makes
the whole thing moot, and just as 99% of post-queryset-refactor code
I've seen has ignored everything except model inheritance (arguably
one of the *less* interesting new features), I expect 99% of
post-newforms-admin code will ignore the interesting feature and
simply turn out to be "automatically fill in the user" tricks.

Meanwhile, in public-facing forms for general (i.e., not necessarily
trusted) users, it's ridiculously easy to automatically fill in a
foreign key automatically.

Richard Dahl

unread,
May 19, 2008, 7:34:44 PM5/19/08
to django...@googlegroups.com
James,
Thanks, this is great. I never made this connection before. I have
been using threadlocals for a while (to implement a custom manager
with role-based access). Since the start I have been passing a users
role as a kw argument in to the Manager via the shell for testing, and
until now it never dawned on me that I could just do that in my views!
Thanks again
-richard
Reply all
Reply to author
Forward
0 new messages