Issue
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.
Solution
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.
Answered By - Peter Rowell
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.