On Mar 9, 2010, at 10:37 AM, James Bennett wrote:

On Tue, Mar 9, 2010 at 8:47 AM, Shawn Milochik <sh...@milochik.com> wrote:
I wonder if this is something that might end up in Django as a built-in
feature at some point. It comes up regularly on this list.

Were I to sit here all morning doing nothing but typing, I wouldn't be
able to say "no" enough times to that.

If you need access to the user somewhere, write a function or method
which takes the user as an argument, and have your view call that
function or method, passing the user. This is approximately eighty
billion times simpler than coming up with threadlocal hacks to try to
create a magic global user variable, results in cleaner,
easier-to-understand code, reduces the likelihood of
difficult-to-track bugs in the code and, best of all, is a perfectly
obvious way to accomplish the task.

And, in all seriousness, when even the page you've linked to says that
it's not a good solution and should be avoided, why do you insist on
continuing to use it? One begins to suspect that some malevolent
entity has been going around magically crippling the fingers of
programmers in such a way that they're no longer capable of writing a
method signature including an argument named "user".


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--


In my particular case, the purpose was to be able to have an audit log of changes made to a model, knowing who made the changes, without having to change all the models and views. I asked around on this list and nobody had a solution for doing this Someone pointed me to the threadlocals page on the official wiki. In a typical case, a user performing an action on a page results in methods being called on a model, some of which cause other model instances to be created or updated. Passing the user around 100% of the time would result in quite a bit of repeated code, and would require every model and every view in every application to be touched.

I'm not saying this to refute your points. I'm saying this because if there's a better way to do this then I'd like to know it. If there's something glaringly obvious that I'm missing, do let me know. Remember that not everyone who works with Django understands it as broadly and in as much depth as you, and realize that just because I may be doing something obviously stupid to you doesn't necessarily mean that I'm being intentionally thick.

Shawn

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to