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

女生的网名这么多〃 提交于 2019-12-25 07:50:21

问题


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?


回答1:


I think that did the trick:

Product.objects.filter(subproducts__isnull=True)


来源:https://stackoverflow.com/questions/15294213/django-get-all-objects-that-dont-belong-to-m2m

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!