[Django]-Django: Group Counts on Foreign Keys

3👍

Using the values() queryset function, you can pull the fields of a foreign key model by using:

.values('foreign__field')

Referring to your example, and assuming your Group model has a “name” field, you can use:

Model.objects.values('group', 'group__name')...

If you pass that dict into the template, then you can refer to the 'group__name' key:

{% for g in groups %}
{{ g.group__name }}

Leave a comment