Reputation: 2365
Suppose I have two Django models, let's say Product and Manufacturer, and wish to find Manufacturers who have no Products.
class Product(Model):
name = models.CharField()
manufacturer = models.ForeignKey(Manufacturer)
class Manufacturer(Model):
name = models.CharField()
I want to generate a set of all Manufacturer objects that are not referred to by any Product objects. I can think of many ways to get this set, but none of them seem clean or Pythonic
eg, I can trivially filter Manufacturers that have at least one Product that satisfies a condition:
models.Manufacturer.objects.filter(product__name=x)
I'd like to be able to filter something more like
models.Manufacturer.objects.exclude(product__exists)
I could probably exclude/filter Manufacturers on some axiom/tautology of Products, but that also doesn't seem very Pythonic.
Upvotes: 3
Views: 288
Reputation:
This can be done using an annotation and a filter:
from django.db.models import Count
idle = Manufacturers.objects.annotate(num_products=Count('product')).filter(num_products=0)
idle
will the contain a list of Manufacturers who have no products, but keep in mind they will also have a property num_products
, which due to the query will be 0.
Upvotes: 2