简体   繁体   English

如何过滤Django QuerySet的相关字段''all'或'none'

[英]How to filter a Django QuerySet's related fields' 'all' or 'none'

Eg I have a model Cat that has a reverse ForeignKey from Life . 例如,我有一个模型Cat ,它有一个来自Life的反向ForeignKey

class Life(models.Model):
    state = models.CharField(choices=('alive', 'dead', 'unknown')
    cat = models.ForeignKey('animals.Cat', related_name="lives")


class Cat(models.Model):
     name = models.CharField(max_length=12)
     cat_type = models.CharField(choices=('normal', 'schroedinger')
     ...

How do I get a QuerySet of Cat s that have lost none of their lives? 如何获得一个没有丢失生命的CatQuerySet Ie have all their lives either in state 'alive' or are of cat_type 'schroedinger' and have none of their lives in state "dead") 即他们的所有生活要么处于“活着”状态,要么属于cat_type“schroedinger”,而且他们的生命中没有一个人处于“死亡”状态。

I haven't used this API in a while, but I believe this will get the job done: 我暂时没有使用过这个API,但我相信这会完成工作:

from django.db.models import Q

normal_and_alive = Q(cat_type="normal") & ~Q(lives__state__in=["dead", "unknown"])
schroedinger_and_not_dead = Q(cat_type="schroedinger") & ~Q(lives__state="dead")

cats = Cat.objects.filter(normal_and_alive | schroedinger_and_not_dead)

See docs for django's docs for complex lookups with the Q() object 有关使用Q()对象进行复杂查找的 django文档,请参阅docs

aside: this will only execute one database query 除此之外:这只会执行一个数据库查询

cats = Cat.objects.filter(id=-1) # make it an empty queryset

temp = Cat.objects.filter(cat_type='normal')
for one in temp:
    cats |= one.lives.filter(state='alive') # set union

temp = Cat.objects.filter(cat_type='schroedinger')
for one in temp:
    cats |= one.lives.exclude(state='dead')

return cats

new answer: 新答案:

cats = Cat.objects.filter(id=-1) # make it an empty queryset

temp = Cat.objects.filter(cat_type='normal')
for one in temp:
    if len(one.lives.exclude(state='alive')) == 0:
        cats |= Cat.objects.filter(id=one.id)

temp = Cat.objects.filter(cat_type='schroedinger')
for one in temp:
    if len(one.lives.filter(state='dead')) == 0:
        cats |= Cat.objects.filter(id=one.id)

return cats

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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