本文介绍了过滤Django ORM中的order_by关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下文中,产品通过贡献者有很多作者,而 contributor.role_code
定义了对产品的确切贡献。 Django ORM是否可以过滤由以下order_by()方法引用的贡献者?例如。我想仅仅通过贡献者订购产品,使得['A01','B01'] 中的 contributor.role_code。
In the below, product has many writers through contributor, and contributor.role_code
defines the exact kind of contribution made to the product. Is it possible with the Django ORM to filter the contributors referenced by the order_by() method below? E.g. I want to order products only by contributors such that contributor.role_code in ['A01', 'B01']
.
Product.objects.filter(
product_type__name=choices.PRODUCT_BOOK
).order_by(
'contributor__writer__last_name' # filter which contributors it uses?
)
推荐答案
要过滤特定值,首先构建您的接受值列表:
To filter on specific values, first build your list of accepted values:
accepted_values = ['A01', 'B01']
然后筛选此列表中的值:
Then filter for values in this list:
Product.objects.filter(
product_type__name=choices.PRODUCT_BOOK
).filter(
contributor__role_code__in=accepted_values
).order_by(
'contributor__writer__last_name'
)
这篇关于过滤Django ORM中的order_by关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!