Django - 过滤外键属性 [英] Django - filtering on foreign key properties
本文介绍了Django - 过滤外键属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图根据外键的特定字段的值来过滤Django中的表。例如,我有两个模型 - Project和Asset,Project有一个名称
字段,每个资产都有一个 ForeignKey(Project)
字段。我想根据相关项目的名称过滤我的资产列表。
目前我正在执行两个查询:
project_list = Project.objects.filter(name__contains =Foo)
asset_list = Asset.objects.filter(desc__contains = filter,project__in = project_list).order_by ('desc')
我想知道是否有办法指定这种过滤主要查询?
解决方案
Asset.objects.filter(project__name__contains =Foo)
I'm trying to filter a table in Django based on the value of a particular field of a foreign key.
For example I have two models - Project and Asset, Project has a name
field and each asset has a ForeignKey(Project)
field. 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屋!
查看全文