Search code examples
pythondjangodjango-admin

Django admin how to show currency numbers in comma separated format


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


Solution

  • 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',)