获取不在django的M2M表中的所有对象

时间:2014-03-04 20:03:44

标签: python django django-models

我有以下型号:

class Invoice(models.Model):
    invoice_number = models.CharField(max_length=100)
    order_items = models.ManyToManyField(OrderItem)

如何获取不在发票中的所有order_items

就像是:

OrderItem.objects.exclude(id__in=invoice) # pseudocode

1 个答案:

答案 0 :(得分:1)

你可以做到

ordered_items_not_in_invoice = OrderItem.objects.filter(invoice__isnull=True)

详情阅读Lookups spanning multi valued relationships__isnull