#35177: Admin.display decorator functions do not output correct-format for 'modern' Python string-formatting -------------------------------------+------------------------------------- Reporter: | Owner: nobody stephenskett | Type: Bug | Status: new Component: | Version: 4.2 contrib.admin | Keywords: admin, display, Severity: Normal | string, format, Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- I am trying to write an admin display function (using the [https://github.com/django/django/blob/main/django/contrib/admin/decorators.py admin.display decorator]) to output the values of a float-field to 3 decimal places.
Seemed like this should be pretty simple, and indeed it is, **IF** you use 'old-style' Python format-strings, e.g. {{{ class ModelFoo_Admin(admin.ModelAdmin): @admin.display def get_value_to_3dp(self, obj): return "%.3f" % obj.bar }}} However, if you use either of the more 'modern'-style Python string- formatting methods (i.e. [https://docs.python.org/3/tutorial/inputoutput.html#the-string-format- method str.format] or [https://docs.python.org/3/tutorial/inputoutput.html #formatted-string-literals formatted string-literals]), the specified string is not displayed using the specified formatting, i.e. this: {{{ class ModelFoo_Admin(admin.ModelAdmin): @admin.display def get_value_to_3dp(self, obj): return "{0:3f}".format(obj.bar) }}} or this: {{{ class ModelFoo_Admin(admin.ModelAdmin): @admin.display def get_value_to_3dp(self, obj): return f'{obj.bar:3f}' }}} display the output string to the default number of decimal-places (in my case, 6), with trailing zeroes, rather than to 3 decimal places as expected. I don't understand why there would be any difference between these approaches, so I am assuming this is a bug rather than the intended behaviour. -- Ticket URL: <https://code.djangoproject.com/ticket/35177> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/0107018d8dc2bc91-00117540-a656-4c0d-95d4-37fd21cff65d-000000%40eu-central-1.amazonses.com.