0

Code snippets:

class HighscoreAdmin(admin.ModelAdmin):
    list_display = ('name', 'score')
    ...

and

class Highscore(models.Model):
    name = models.CharField(max_length=128)
    score = models.PositiveIntegerField(default=0)
    ...

Question:

Let's say we have a score value of 100000. In Admin, it will display the score as 1000000, but what if I want a different format? How would you change the format to output -> 1,000,000?

I heard that you could add a method to the Highscore class something like foo_display, where foo is the field in question:

def score_display(self):
    """ returns scores with commas as thousands separators in the admin display """
   return '{:,}'.format(self.score)
Amphagory
  • 51
  • 9
  • This has been asked and answered [here](https://stackoverflow.com/questions/7216764/in-the-django-admin-site-how-do-i-change-the-display-format-of-time-fields) before. – ferrix Feb 20 '19 at 22:11
  • Possible duplicate of [In the Django admin site, how do I change the display format of time fields?](https://stackoverflow.com/questions/7216764/in-the-django-admin-site-how-do-i-change-the-display-format-of-time-fields) – ferrix Feb 20 '19 at 22:11
  • @ferrix Not the duplicate of that post. – Masood Khaari Oct 09 '21 at 00:15
  • For those still interested, [check this](https://stackoverflow.com/questions/3203089/make-django-forms-use-comma-as-decimal-separator). – Masood Khaari Oct 09 '21 at 00:16

1 Answers1

0

You can use NUMBER_GROUPING and USE_THOUSAND_SEPARATOR in settings.py:

USE_L10N = True

USE_THOUSAND_SEPARATOR = True
NUMBER_GROUPING = 3

(Also check THOUSAND_SEPARATOR and DECIMAL_SEPARATOR.)

Masood Khaari
  • 2,911
  • 2
  • 23
  • 40