Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Access request object in model's hooks and display methods [RSS Feed]

#1 Nov. 17, 2005 10:09:33

p.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access request object in model's hooks and display methods


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 ?

Offline

#2 Nov. 17, 2005 11:40:38

l.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access request object in model's hooks and display methods


2005/11/17, plisk <>:
>
> 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.

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

Offline

#3 Nov. 17, 2005 13:29:36

p.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access request object in model's hooks and display methods


limodou wrote:
> 2005/11/17, plisk <>:
> >
> > 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__
if isinstance(klass, meta.ModelBase):
klass.request = request

return None

Offline

#4 Nov. 17, 2005 14:29:03

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access request object in model's hooks and display methods


On 11/17/05, plisk <> 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

Offline

#5 Nov. 17, 2005 20:54:12

Ian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access request object in model's hooks and display methods


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?

On 11/18/05, Adrian Holovaty <> wrote:
>
> On 11/17/05, plisk <> 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
>


--
-- ++61-3-9877-0909
If everything seems under control, you're not going fast enough. -
Mario Andretti

Offline

#6 Nov. 20, 2005 23:17:09

r.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Access request object in model's hooks and display methods


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 onlyin Web context(as for now?) and seems accessing request object fromthere shouldn't make too much harm to models design ?plisk

Offline

  • Root
  • » Django
  • » Access request object in model's hooks and display methods [RSS Feed]

Board footer

Moderator control

Enjoy the 23rd of January
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support