[Answered ]-How to make the following query in Django?

2đź‘Ť

âś…

Queryset filters can be chained.

The result of refining a QuerySet is itself a QuerySet, so it’s
possible to chain refinements together.

teachers = User.objects.filter(groups__name='Teacher').filter(profile__school=school)

It’s also possible to add to conditions inside the filter

teachers = User.objects.filter(groups__name='Teacher',profile__school=school)

Update to clarify point raised in comment

This sort of thing works because foreign key looks works, backwards too. To quote from the manual:

It works backwards, too. To refer to a “reverse” relationship, just
use the lowercase name of the model.

The original query you used referred to the group table this way.

👤e4c5

Leave a comment