Access request object in model's hooks and display methods

84 views
Skip to first unread message

plisk

unread,
Nov 17, 2005, 5:09:13 AM11/17/05
to Django users
Hi there.

Seems like its not possible to access request object in hooks like
pre_save and display methods(those are listed in list_display) ? Is it
supposed to be like this by design ? If so then hardly its very
convinient to use in real applications. For example, i would like to
get server address and port in post_save hook, current logged in user
in display method and so on..

Or maybe there is the way ?

limodou

unread,
Nov 17, 2005, 6:40:34 AM11/17/05
to django...@googlegroups.com
2005/11/17, plisk <rot...@gmail.com>:
May be middleware can do this. But I'm not sure.

--
I like python!
My Blog: http://www.donews.net/limodou
NewEdit Maillist: http://groups.google.com/group/NewEdit

plisk

unread,
Nov 17, 2005, 8:29:25 AM11/17/05
to Django users
limodou wrote:
> 2005/11/17, plisk <rot...@gmail.com>:
> >
> > Hi there.
> >
> > Seems like its not possible to access request object in hooks like
> > pre_save and display methods(those are listed in list_display) ? Is it
> > supposed to be like this by design ? If so then hardly its very
> > convinient to use in real applications. For example, i would like to
> > get server address and port in post_save hook, current logged in user
> > in display method and so on..
> >
> > Or maybe there is the way ?
> >
> >
>
> May be middleware can do this. But I'm not sure.

Yeah, finally came up with this dirty hack as a middleware

from django.core import meta
import django.models.intranet

class ModelsAddRequest:
def process_request(self, request):
for x in dir(django.models.intranet):
klass = django.models.intranet.__dict__[x]
if isinstance(klass, meta.ModelBase):
klass.request = request

return None

Adrian Holovaty

unread,
Nov 17, 2005, 9:28:40 AM11/17/05
to django...@googlegroups.com
On 11/17/05, plisk <rot...@gmail.com> wrote:
> Seems like its not possible to access request object in hooks like
> pre_save and display methods(those are listed in list_display) ? Is it
> supposed to be like this by design ? If so then hardly its very
> convinient to use in real applications.

Yes, this is by design. Models have no knowledge of whether they're
being used in a Web context, or in a shell script, or in a desktop GUI
app. Coupling models to Web requests would be convenient in some cases
but a bad design decision overall.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Ian Holsman

unread,
Nov 17, 2005, 3:53:42 PM11/17/05
to django...@googlegroups.com
couldn't we do something similar to get_active_site() call on the sites table?

that way people who need the 'request' object could just call it and
it wouldn't impact the api of anything else?
--
I...@Holsman.net -- ++61-3-9877-0909
If everything seems under control, you're not going fast enough. -
Mario Andretti

rotmer

unread,
Nov 20, 2005, 6:17:37 PM11/20/05
to django...@googlegroups.com
Adrian Holovaty wrote:
>> Seems like its not possible to access request object in hooks like
>> pre_save and display methods(those are listed in list_display) ? Is it
>> supposed to be like this by design ? If so then hardly its very
>> convinient to use in real applications.
>
> Yes, this is by design. Models have no knowledge of whether they're
> being used in a Web context, or in a shell script, or in a desktop GUI
> app. Coupling models to Web requests would be convenient in some cases
> but a bad design decision overall.

I see. As for save hooks i agree, but display methods anyway used only
in Web context(as for now?) and seems accessing request object from
there shouldn't make too much harm to models design ?

plisk

Reply all
Reply to author
Forward
0 new messages