tunarob
tunarob

Reputation: 3038

Django - get all objects that don't belong to M2M

I have a model with field:

class Product(models.Model):
    subproducts = models.ManyToManyField("self", blank=True)

I need to overwrite admin's field queryset, to display only that objects that don't belong to any m2m relation. I have no idea how to get them.

So if I have: product1, product2, product3, product4.

product1 contains in subproducts: product2

I need a query that will get, in that situation, product3 and product4

Any idea how to get that?

Upvotes: 0

Views: 247

Answers (1)

tunarob
tunarob

Reputation: 3038

I think that did the trick:

Product.objects.filter(subproducts__isnull=True)

Upvotes: 2

Related Questions