Django admin:如何显示固定长度的数字?

Django admin: how to display number with fixed length?

这是我的模型:

from django.contrib.humanize.templatetags.humanize import intcomma


class Flow(models.Model):
    amount = models.DecimalField(max_digits=10, decimal_places=2)
    
    def df_amount(self):
        return '{intcomma(abs(self.amount)):>12}'
    df_amount.admin_order_field = 'amount'
    df_amount.short_description = 'amount'

admin.py,

@admin.register(Flow)
class FlowAdmin(admin.ModelAdmin):
    list_display = (
        'df_amount',
    )

对于amount=2800print(self.df_amount())给出$ 2,800.00 但是 $ 2,800.00 显示在管理面板中,中间的 space 被截断为只有一个 space,与预期不同。

所以我的问题是如何在管理面板中保留字符串中间的spaces?谢谢!

可以使用format_html(...)函数,

from django.utils.html import format_html


@admin.register(Flow)
class FlowAdmin(admin.ModelAdmin):
    list_display = (
        'df_amount',
    )

    def df_amount(self, instance):
        <b>return format_html(f'$ {instance.df_amount().replace(" ", "&nbsp;")}')</b>