User/UserProfile Queryset problem

52 views
Skip to first unread message

bcrem

unread,
Mar 26, 2012, 9:15:04 PM3/26/12
to Django users
Hello Django Nerds!

So I have a (somewhat) complicated lookup I'm trying to do; here's the
gist of it:

1. I have a Store class, with a User ManyToManyField, Store.users
2. I have a user profile class associated with each user,
UserProfile, accessible through the usual User.get_profile()
3. UserProfile has a status variable, UserProfile.status, which can
be 'Active', 'Inactive', or 'Deleted'

I'm trying to display a list of users for a particular store;
currently I generate this list using the following line:

userList = request.session['currentStore'].users.all()

Works great; however, now I'd like to filter out all users with a
status of 'Deleted'. Now, if status were a User attribute, I could
just try this:

userList =
request.session['currentStore'].users.exclude(status=='Deleted')

or something similar.

THE PROBLEM: How do I generate this no-deleted-users list for the
given store, using the ManyToManyField Store.users, but based on the
store.users UserProfile.status? I know there's some kinky django
black magic reverse-lookup way to do it in a single magnificent
line...just haven't a clue what it might be. Ideas?

First person with the right answer: thank you - please treat yourself
to a donut...

Daniel Roseman

unread,
Mar 27, 2012, 2:31:43 AM3/27/12
to django...@googlegroups.com
The session seems to be a red herring here. You're just doing a filter across a related field, which is done with the normal double-underscore syntax:

    current_store.users.exclude(userprofile__status='Deleted')
--
DR.

bcrem

unread,
Mar 27, 2012, 9:43:12 AM3/27/12
to Django users
Oh. I see. So...that wasn't very complicated at all then, was it?

Thanks DR - didn't realize you could access userprofile directly from
user that way.
Reply all
Reply to author
Forward
0 new messages