我想为我的采购模型创建一个模型管理器:
class Purchase(models.Model)
number = models.IntegerField
class InventoryLog(models.Model)
purchase = models.ForeignKey(Purchase)
sold_out = models.BooleanField(default=false)
我希望我的模型管理器返回与 sold_out
值为 的
InventoryLog
对象无关的任何 Purchase
对象>真
有没有办法用查询集、Q 对象或 F 对象来处理这个问题,还是我需要求助于 for 循环?
最佳答案
我相信 Purchase.objects().exclude(inventorylog__sold_out=True)
会做到这一点。
关于Django Querysets - 按相关对象中的 bool 值过滤对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708442/