以下所有代码均为实际代码的简化表示。
如果我有以下Django模型:
class Material(models.Model):
name = models.CharField(max_length=110)
class OrderDetail(models.Model):
material = models.ForeignKey(Material)
order_quantity = models.IntegerField()
quantity_delivered = models.IntegerField(null=True, blank=True)
is_open = models.BooleanField()
is_active = models.BooleanField()
class MaterialRequirement(models.Model):
material = models.ForeignKey(Material)
quantity = models.IntegerField()
我手头有一个名为
material_requirement
的MaterialRequirement
模型实例。我想要检索符合以下条件的OrderDetail
对象:1. material = material_requirement.material
2. is_open = True
3. is_active = True
4. order_quantity > quantity_delivered
5. material_requirement.quantity <= order_quantity - quantity_delivered
我的问题是如何在下面的ORM查询中加入第5个选择条件。
order_details = OrderDetail.objects.select_related('material').filter(material=material_requirement.material,
is_open=True,
is_active=True,
order_quantity__gt=F('quantity_delivered'))