Reverse query on django extended user profile
I have extended User
via AUTH_PROFILE_MODULE
to include a ManyToMany
field to Projects the user is involved in.
I want to reverse the relationship, and get the Users associated with a given Project.
My users have profiles (I've successfully returned them with related_projects = u.profile.projects
).
related_users = project.userprofile_set.all()
is giving me something - {% for user in related_users %} {{ user }} {% endfor %}
yields User Profile for: richlyon
. But I can't get {{ user }}
to yield any fields. I've tried {{ user.username }}
.
Question: am I going about this the wrong way? And if I'm not, how do I get field information out o开发者_如何学运维f a reverse relationship on an extended User profile?
Thanks as always for your time.
Your 'profile' object is not a User
, nor is it extending User
(in an object-oriented sense). Rather, it's a separate model that has a ForeignKey
to a User
.
User > Profile > Projects
In the code you posted, it looks like you're expecting a User
object to be returned from the project.userprofile_set.all()
query set. However, these are going to be your intermediate profile objects from which you can then access the user.
Something like this:
related_profiles = project.userprofile_set.all()
{% for profile in related_profiles %}
{{ profile.user.username }}
{% endfor %}
精彩评论