Accessing users groups through attributes / foreign keys in Django
<p>I'm trying to access my users groups in Django, but keep getting</p>

<pre><code>auth.Group.None
</code></pre>

<p>as the result.</p>

<p>When in the django shell I'll first create a group, then a user and add the user to the group. After this, I try to print the users groups, like so:</p>

<pre><code>from django.contrib.auth.models import User, Group

Artists = Group.objects.create(name="Artists")
Artists.save()
Rihanna = User.objects.create_user(username="Rihanna", password="newpassword")
Rihanna.save()
Artists.user_set.add(Rihanna)
# Save both of them again just to double check
Rihanna.save()
Artists.save()

# Now try to access Rihanna's groups through her attributes:
print Rihanna.groups, Rihanna.groups.name
</code></pre>

<p>and the results are</p>

<pre><code>&gt;&gt;&gt; auth.Group.None None
</code></pre>

<p>What I'd like to happen is for it to print the groups name.</p>

<p>I know I can query the members of a group by the filter and get commands, but I'd like to access the groups straight through a users attributes/foreign keys. Any idea what I'm doing wrong?</p>

<p>You added user to group in group object, but your user object does not have update state for this.</p>

<p>Try access groups like this:</p>

<pre><code>print Rihanna.groups.all()
</code></pre>

<p>or add group in user model:</p>

<pre><code>Rihanna.groups.add(Artists)
</code></pre>

python django