Django select_related过滤器 [英] Django select_related filter

查看:188
本文介绍了Django select_related过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Django模型。

I have the following Django models.

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

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

现在我有以下查询。

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

现在,这将获取B的所有对象。现在如何包含 active = True archived = False的过滤器在模型 B select_related 子句中。

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.

推荐答案

与其他任何相关字段一样,使用 __ 查找。.

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)

在这里使用select related不会改变任何内容,其目的是t返回的信息是什么,对过滤完全没有影响。

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.

这篇关于Django select_related过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆