2

I am developing a web application that displays an html table using django, django-tables2 and Bootstrap4. I have a column AUM which contains very large numbers (up to billions). In my models.py the corresponding model uses a models.Interfield() for AUM.

class MyModel(models.Model):
    ...
    AUM = models.IntegerField(null= True, blank= True)
    ...

Using django-tables2 and Bootstrap4 this model gets transformed into a table and rendered into a template using

{% render_table table %}

The numbers displayed in the corresponding column a displayed in there raw format which looks like e.g. this 1000000000. I want to make it more human readable. I have found two solutions for seperating thousands

I have also seen that there is something similar to intcomma which is intword and it transforms e.g. 1000000 into 1 Million which is in my opinion even more human readable. As with the intcomma this is not a viable solution for me, which is why I am looking for a global setting like USE_THOUSAND_SEPARATOR = True however for displaying 1000000 as 1 Million (or Mio.) not 1,000,000.

Jieter
  • 4,101
  • 1
  • 19
  • 31
deepNdope
  • 179
  • 3
  • 14

1 Answers1

0

Django-tables2 allows using a TemplateColumn to use the django templating language for a cell. This does require you to create a custom table:

# settings.py

INSTALLED_APPS = (
    # ...
    "django.contrib.humanize",
    "django_tables2"
    # ...
)


# tables.py

import django_tables2 as tables


class 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.py

class MyView(SingleTableView):
    table_class = MyTable
Jieter
  • 4,101
  • 1
  • 19
  • 31