简体   繁体   English

Django select_related过滤器

[英]Django select_related filter

I have the following Django models. 我有以下Django模型。

class A(models.Model):
    tmp = models.ForeignKey(B)
    active = models.BooleanField()

class B(models.Model):
    active = models.BooleanField()
    archived = models.BooleanField()

Now I have the following query. 现在我有以下查询。

A.objects.select_related(B).filter(active=True)

Now this fetches all the objects of B. Now how can I include a filter of active=True and archived=False in the select_related clause for model B . 现在,这将获取B的所有对象。现在,如何在模型Bselect_related子句中包含active=Truearchived=False的过滤器。

The same as you would with any other related field, with a __ lookup.. 与使用__查找的任何其他相关字段相同

A.objects.select_related(B).filter(active=True, tmp__active=True, tmp__archived=False)

Using select related doesn't change anything here, its purpose is about what information is returned with the results, it has no effect on filtering at all. 使用select related不会在这里改变任何东西,它的目的是关于结果返回什么信息,它根本不影响过滤。

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

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