Skip to content Skip to sidebar Skip to footer

Django Queryset On Related Field With Multiple Constraints

Suppose I have the following models: class User(models.Model): # ... some fields class Tag(models.Model): # ... some fields class UserTag(models.Model): user = models

Solution 1:

Your filter() call can include multiple constraints:

User.objects.filter(tags__tag=given_tag, tags__date_removed=None)

When they match, they will both match to the sameTag, not two possibly different ones.

See the documentation on spanning multi-valued relationships; in particular, the difference between filter(a, b) and filter(a).filter(b).

Post a Comment for "Django Queryset On Related Field With Multiple Constraints"