[Answered ]-How to select users based on their profile

0👍

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.

2👍

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

Leave a comment