Tuesday, March 27, 2012

Re: User/UserProfile Queryset problem

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.

On Mar 27, 2:31 am, Daniel Roseman <dan...@roseman.org.uk> wrote:
> On Tuesday, 27 March 2012 02:15:04 UTC+1, bcrem wrote:
>
> > 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...
>
> 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.

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

No comments:

Post a Comment