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
- Given the queryset of all Products, generate a set of all Manufacturers that have products. Then take the set difference of all manufacturers with the manufacturers_with_products as described here: How to get the difference of two querysets in Django.
- Filter the Manufacturer objects based on the lack of a reverse relationship with any Products. Django makes it easy to filter based on properties of reverse relationships, but I cannot find a clean way to filter based on the existence of this reverse relationship.
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.