【问题标题】:How to filter a Django QuerySet's related fields' 'all' or 'none'如何过滤 Django QuerySet 的相关字段“全部”或“无”
【发布时间】:2013-07-30 03:13:37
【问题描述】:

例如,我有一个模型 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')
     ...

我如何获得没有失去生命的Cats 中的QuerySet? IE。 所有他们的生命要么处于“活着”状态,要么属于 cat_type 'schroedinger',并且没有任何生命处于“死亡”状态)

【问题讨论】:

    标签: python django django-queryset django-orm


    【解决方案1】:

    我有一段时间没有使用这个 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)
    

    查看 django 的文档以获取 complex lookups with the Q() object 的文档

    除此之外:这只会执行一个数据库查询

    【讨论】:

      【解决方案2】:
      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
      

      新答案:

      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
      

      【讨论】:

      • 我不确定,可能是猫的数量+3
      • 如果将 state 字段移动到 Cat,点击次数会少很多
      • 这段代码不会运行,one.lives.filter(...) 返回一个Life() 记录的查询集,你不能这样做,因为它本质上是Cat.objects.all() | Life.objects.all()。它不起作用,因为它们在不同的表中
      • @Jiaaro,哦,是的,你是对的,我被投赞成票仅仅是因为我知道一点物理学来理解提问者想要什么。
      猜你喜欢
      • 2022-12-10
      • 2019-11-10
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-11-24
      • 2019-01-15
      • 1970-01-01
      • 2018-12-30
      相关资源
      最近更新 更多