开发者

How to select users based on their profile

I have a complicated query built up based on a users profile, I start with

qset = Profile.objects

bunch of stuff that works to return me profile objects (it uses Q objects, and optionally ignores some fields if they were left blank)

I could grab the users开发者_开发技巧 with selected_related() but that still leaves me with a list of profiles, rather than a list of users.

Because of the way my templates are set up for other things, I'd really like to have a list of users

{% for user in users %}

How can I convert his queryset for Profile objects into one for Users.

Currently I use:

profile_userids = list(qset.values('user_id'))
user_ids = [d['user_id'] for d in profile_userids]
users = User.objects.in_bulk(user_ids)

which results in 2 queries, and the conversion of all the user_id's into python objects.

How can I use the queryset that I have generated on the Profiles object to select users?


Make your Q objects refer to profile__whatever and use them in User.objects.filter().


Turns out I had to mod the templates. There is a bug in Django auth.user. When the view code looks like:

@login_required
def test(request):
    a = User.objects.filter(pk=request.user.id).select_related('profile').get()
    return render_to_response('test.html', {'a':a,})

and the template looks like

a.username {{ a.username }}<br />
a.get_profile.age {{ a.get_profile.age }}<br />

a.get_profile ignores the fact that the profile was loaded with select_related, and does a separate query for the profile.

However, if you code it like:

@login_required
def test(request):
    a = Profile.objects.filter(user=request.user).select_related('user').get()
    return render_to_response('peeks/test.html', {'a':a,})

and the template like:

a.user.username {{ a.user.username }}<br />
a.age {{ a.age }}<br />

then you get it all with one select, and when you are displaying 25 profiles on one page, that makes a fair amount of difference.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜