亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Django:訂單對象取決于兩個值

Django:訂單對象取決于兩個值

喵喔喔 2023-07-11 17:08:00
所以我有這個:class Thread(models.Model):    first_thread_notification = models.IntegerField(default=0)    second_thread_notification = models.IntegerField(default=0)我需要根據兩個對象的總和對對象進行排序:class Meta:    ordering = ['-first_thread_notification' + '-second_thread_notification']我知道這是不正確的,但我該怎么做呢?編輯class ManagerSum(models.Manager):    Thread.objects.annotate(       total=ExpressionWrapper(       F('first_thread_notification') + F('-second_thread_notification'),        output_field=IntegerField(),       )    ).order_by('-total')class Thread(models.Model):    first_thread_notification = models.IntegerField(default=0)    second_thread_notification = models.IntegerField(default=0)    total_notifications = ManagerSum()class Meta:    ordering = ['-total_notifications']它是否正確?
查看完整描述

1 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

您可以使用 annotate通過 F 表達式對它們求和。

from django.db.models import IntegerField, ExpressionWrapper

Thread.objects.annotate(

? ? total=ExpressionWrapper(

? ? ? ? F('first_thread_notification') + F('-second_thread_notification'),?

? ? ? ? output_field=IntegerField(),

? ? )

).order_by('-total')


查看完整回答
反對 回復 2023-07-11
  • 1 回答
  • 0 關注
  • 137 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號