Custom model initializers - getting logged in user in django models

78 views
Skip to first unread message

kahless

unread,
Feb 17, 2007, 7:42:25 AM2/17/07
to Django users
hi,

i am creating a very simple board and wanted to add a method to my
models which would check if there are any new posts in a thread /
forum for a given user.. for this to work .. i obviously need the
currently logged in user in my models ..

but i didn't want to iterate through all my models in my view, since i
used the django.views.generic.list_detail.object_list which does the
paging and everything for me ..

so i thought the easiest way would be to allow adding of custom
initializers.. which would be called once a model was created by
django.db.models.query.QuerySet .. so i played around a little until
it worked for me: http://yourhell.com/~kahless/query_initializers_patch.diff

the usage is quite simple:
class MyModelInitializer(object):
def __init__(self, request):
self.request = request

def init_model(self, model):
model.do_init( self, self.request.user )

(my models would have a 'do_init' method which takes the user and
stores it internally)

the object_list would then get a queryset argument in my view like:
queryset =
Post.objects.filter( ..... ).add_initializer( MyModelInitializer( request ) )

and my templates could simply call {% if thread.hasNewPosts %} ...

so .. is this a clean solution ? or can this be done differently ?
and .. would there any chance this patch could find it's way into
django or does it break any main design philosophy ?

- i'm very new to python and django alike .. so there might be an
obvious other solution.

thanks & cu,
Herbert Poul

Honza Král

unread,
Feb 17, 2007, 9:22:50 AM2/17/07
to django...@googlegroups.com
This doesn't seem very clean to me...

I also need to have logged-in user available in my models, so I have
written a custom middleware that stores the user in thread_locals:

import threading
_thread_locals = threading.local()

def get_current_user():
return getattr( _thread_locals, 'user', None )

class RequestMiddleware( object ):

def process_request( self, request ):

_thread_locals.user = request.user

then, I can use get_current_user() anywhere in my application and it
will provide me with the current user, for some fields I even have
something like:

user = models.ForeignKey( User, default=get_current_user )

I cannot take credit for this, I found it on

http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser


--
Honza Král
E-Mail: Honza...@gmail.com
ICQ#: 107471613
Phone: +420 606 678585

kahless

unread,
Feb 17, 2007, 10:19:29 AM2/17/07
to Django users
altough i'm not sure if thread locals are that clean .. i guess it's a
better way ..
thanks .. i was searching for something like that .. i wonder why i
haven't found that site :(
it seems there are tons more documentation in the wiki as i've
thought ;)

cu,
herbert

On Feb 17, 3:22 pm, "Honza Král" <honza.k...@gmail.com> wrote:
> This doesn't seem very clean to me...
>
> I also need to have logged-in user available in my models, so I have
> written a custom middleware that stores the user in thread_locals:
>
> import threading
> _thread_locals = threading.local()
>
> def get_current_user():
> return getattr( _thread_locals, 'user', None )
>
> class RequestMiddleware( object ):
>
> def process_request( self, request ):
>
> _thread_locals.user = request.user
>
> then, I can use get_current_user() anywhere in my application and it
> will provide me with the current user, for some fields I even have
> something like:
>
> user = models.ForeignKey( User, default=get_current_user )
>
> I cannot take credit for this, I found it on
>
> http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
>

> Honza Kr?l
> E-Mail: Honza.K...@gmail.com

Reply all
Reply to author
Forward
0 new messages