使用django ORM,是否有办法限制或过滤order_by适用的范围?
Product.objects.filter(
product_type__name=choices.PRODUCT_BOOK
).order_by(
# need to order these last names by those
# who have a role code of "A01", then "B01"
'contributor__writer__last_name'
)
即使使用原始SQL,也无法仅对某些行应用订单。因此,最好的方法是将alls排序并按role_code分组。
另外,您可以使用role_code
"A01"
或来贡献者"B01"
,对他们进行订购,然后获得其余贡献者(不包括role_code
if"A01"
或)"B01"
。然后合并查询结果。
order_products = Product.objects.filter(
product_type__name=choices.PRODUCT_BOOK
).filter(contributor__role_code__in=['A01','B01'])
.order_by(
# need to order these last names by those
# who have a role code of "A01", then "B01"
'contributor__writer__last_name'
)
non_order_products = Product.objects.filter(
product_type__name=choices.PRODUCT_BOOK
).exclude(contributor__role_code__in=['A01','B01'])
final_result = order_products | non_order_products
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句