简体   繁体   中英

Django: filtering model from related field

In Django I have the following models: 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM