Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (19.9k points)

With django auth group and user models, I want to query all groups with their users. (user_id list would be fine).

I tried

Group.objects.all().select_related('user')

But it isn't working:

FieldError: Invalid field name(s) given in select_related: 'user'. Choices are: (none)

1 Answer

0 votes
by (25.1k points)

I think this would satisfy your requirement :)

from django.contrib.auth.models import Group

group_user_dict = {group.name: group.user_set.values_list('id', flat=True) for group in Group.objects.all()}

Related questions

+1 vote
2 answers

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...