Django - 过滤外键属性 [英] Django - filtering on foreign key properties

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

问题描述

我正在尝试根据 ForeignKey 的特定字段的值过滤 Django 中的表.

I'm trying to filter a table in Django based on the value of a particular field of a ForeignKey.

例如,我有两个模型:

class Asset(models.Model):
    name = models.TextField(max_length=150)
    project = models.ForeignKey('Project')

class Project(models.Model):
    name = models.TextField(max_length=150)

我想根据关联项目的名称过滤我的资产列表.

I'd like to filter my asset list based on the name of the associated project.

目前,我正在执行两个查询:

Currently, I am performing two queries:

project_list = Project.objects.filter(name__contains="Foo")
asset_list = Asset.objects.filter(desc__contains=filter,
                                  project__in=project_list).order_by('desc')

我想知道是否有办法在主查询中指定这种过滤?

I'm wondering if there is a way to specify this kind of filtering in the main query?

推荐答案

Asset.objects.filter( project__name__contains="Foo" )

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

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