Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a query by property of user profile in Django?

Tags:

python

django

I have two models,Design and Profile. Profile is hooked up in settings.py as the profile to be used with the User model. So I can access it via user.get_profile().

And each Design instance has an author property that is a ForeignKey to User.

So, when I'm any view, I can get the screenname (a property of Profile) by:

user.get_profile().screenname

But what is the syntax to SEARCH BY FILTER for this property? What I currently have:

designs = Design.objects.filter(author__userprofile__screenname__icontains=w)

This doesn't work. Thoughts?

like image 254
Sebastian Avatar asked Mar 10 '10 22:03

Sebastian


1 Answers

If your profile class is named Profile, and you haven't customized the User <-> Profile relation using the related_name property of the ForeignKey, then shouldn't you be accessing via:

designs = Design.objects.filter(author__user__profile__screenname__icontains=w)

The User -> Profile spans a relation so you need the extra double underscores.

like image 195
stevesw Avatar answered Oct 05 '22 00:10

stevesw