在 Django 中将大量减少到 intword 而不编辑模板

我正在开发一个使用 django、django-tables2 和 Bootstrap4 显示 html 表的 Web 应用程序。我有一列AUM包含非常大的数字(高达数十亿)。在我models.py的相应模型中使用models.Interfield()for AUM。


class MyModel(models.Model):

    ...

    AUM = models.IntegerField(null= True, blank= True)

    ...

使用 django-tables2 和 Bootstrap4,这个模型被转换成一个表格并使用


{% render_table table %}

显示在相应列 a 中的数字以原始格式显示,例如这个 1000000000。我想让它更易读。我找到了两个用于分隔数千个的解决方案

我也看到有类似的东西intcommaintword并把它转换例如百万到1万元,在我看来更是人类可读的。如同intcomma这不是我一个可行的解决方案,这就是为什么我要寻找一个全局设置类似USE_THOUSAND_SEPARATOR = True但是显示百万为1万美元(或Mio.)不1,000,000。


慕莱坞森
浏览 162回答 1
1回答

精慕HU

Django-tables2 允许使用 aTemplateColumn为单元格使用 django 模板语言。这确实需要您创建一个自定义表:# settings.pyINSTALLED_APPS = (    # ...    "django.contrib.humanize",    "django_tables2"    # ...)# tables.pyimport django_tables2 as tablesclass MyTable(tables.Table):    AUM = tables.TemplateColumn(        template_code="{% load humanize %}{{ value | intword }}"    )    class Meta:        model = MyModel        sequence = ("AUM", "...")  # using "..." here will automatically add the remaining columns of the model to the table.# views.pyclass MyView(SingleTableView):    table_class = MyTable
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java