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을 사용하더라도 일부 행에만 순서를 적용 할 수 없습니다. 따라서 가장 좋은 방법은 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] 삭제
몇 마디 만하겠습니다