views:

36

answers:

1

I'd like to do something like this:

class Task(models.Model): ... created_by = models.ForeignKey(User, default=[LoggedInUser] blank=True, null=True, related_name='created_by')

Is this possible? I couldn't find what's the proper way to get the logged in user, apart from doing request.user, in a view, which doesn't seem to work here.

PS_ I realise I could initialize the Model data by other means, but I think this is the cleanest way.

+1  A: 

No, you can't do it this way. Django (and Python) has pretty much zero global values, and that's a Good Thing(tm). Normally you get the current user in the view(request) with request.user. You can then pass that as a param to various methods/functions, but trying to set a global user will only lead to tears in a multi-threaded environment.

There should be a bumper sticker that says, Globals are Evil. This will give you a good idea about my Number One problem with PHP.

Peter Rowell
Fair enough. I'll see if I can get it to work with signals, which I think it may be the best way to go then. Thanks
ign