简体   繁体   中英

Django - complicated ORM Query (related_set)

I have model Car, Listing and Website.

class Listing..
    car = ForeignKey...
    website = ForeignKey...
    active = BooleanField...

I want to filter all car objects that have listing object on the given website but the listing needs to be active.

To get all car objects that have listing object on the given website:

website = ...
Car.objects.filter(listings__website__in=website)

But how do I filter out inactive listings?

I want something like:

website = ...
Car.objects.filter(listings[only_active]__website__in=website)

You would do an extra condition on your filter:

Car.objects.filter(listings__website__in=website, listings__active=True)

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