在Django内置管理站点中,如何在“更改列表”页面和“更改表单”页面中将进度栏显示为字段?
答案 0 :(得分:2)
例如:
percentage_paid
,以计算结果并呈现HTML元素。# models.py
class SomeModel(models.Model):
....
def percentage_paid(self):
if self.paid_amount and self.final_price:
percentage = round((self.paid_amount / self.final_price * 100), 2)
else:
percentage = 0
return format_html(
'''
<progress value="{0}" max="100"></progress>
<span style="font-weight:bold">{0}%</span>
''',
percentage
)
'percentage_paid'
添加到readonly_filds
list_display
中,如果要在更改表单页面中显示,请添加到fields
中。我将其添加到fieldsets
中以自定义我的更改表单。# admin.py
class CaseAdmin(CommonInfoAdmin):
fieldsets = CommonInfoAdmin.fieldsets + (
('Sales Summary', {
'fields': (
....
('final_price', 'paid_amount', 'balance', 'percentage_paid'),
....
)
}),
)
readonly_fields = [
....
'percentage_paid',
]
list_display = (
....
'percentage_paid'
)
结果: