相关疑难解决方法(0)

django admin启用对计算字段的排序

我的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语句进行排序?

python django django-admin

8
推荐指数
1
解决办法
1927
查看次数

标签 统计

django ×1

django-admin ×1

python ×1