Home > Software design >  Django admin how to show currency numbers in comma separated format
Django admin how to show currency numbers in comma separated format

Time:08-28

In my models I have this:

class Example(Basemodel):
       price = models.IntegerField(default=0)

and in my admin I have this:

@admin.register(Example)
class ExampleAdmin(admin.ModelAdmin):
    list_display = ('price',)

I want the price field to be shown in comma-separated format instead of the typical integer format and I want to do that on the backend side. For example: 333222111 should be 333,222,111 Any can recommend me a solution?

enter image description here

Should be:

enter image description here

CodePudding user response:

You can work with a property instead, for example:

from django.contrib import admin


class Example(Basemodel):
    price = models.IntegerField(default=0)

    @property
    @admin.display(description='price', ordering='price')
    def price_formatted(self):
        return f'{self.price:,}'

and use that property:

@admin.register(Example)
class ExampleAdmin(admin.ModelAdmin):
    list_display = ('price_formatted',)

CodePudding user response:

try thousand seperator in your settings.py

USE_THOUSAND_SEPARATOR = True

alternatively you can use intercoma to convert integer to a string containing commas every three digits

  • Related