[英]Django: filtering model from related field
In Django I have the following models: Person , Device , Consumption 在Django中,我有以下模型: Person , Device , Consumption
A Person can have zero to many Devices
一个人可以拥有零到许多设备
A Device can have zero to many Consumptions设备的 消耗量为零到很多
How can I filter Persons on a value in Consumption? 如何根据“消费”中的值过滤人?
I can filter on fields from Devices: 我可以过滤设备中的字段:
Person.objects.filter(device__is_active=True)
But how can I filter on Consumption fields? 但是,如何过滤“消费”字段?
要获取一个Consumption
对象(在本例中为其ID),请执行以下操作:
Person.objects.filter(device__consumptions__id=consumption_id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.