我的db表和模型中有以下两个字段(Model Name:Order):
id, branch_id, product_id, cost, quantity, status, ordered_at
Run Code Online (Sandbox Code Playgroud)
我在OrderModelAdmin中有以下代码:
list_display = (
'order_number',
'branch',
'product',
'cost',
'quantity',
'calculated_total',
'status',
'ordered_at',
)
def calculated_total(self, obj):
return obj.cost * obj.quantity
calculated_total.short_description = _('Total')
Run Code Online (Sandbox Code Playgroud)
现在,我想为此字段启用排序.实际上,我需要做的就是在SELECT语句中添加一列:
SELECT (t.cost * t.quantity) as TOTAL
ORDER BY TOTAL
Run Code Online (Sandbox Code Playgroud)
有没有办法可以在Django Admin中附加SQL语句进行排序?