[Django] #35177: Admin.display decorator functions do not output correct-format for 'modern' Python string-formatting

5 views
Skip to first unread message

Django

unread,
Feb 9, 2024, 7:06:10 AMFeb 9
to django-...@googlegroups.com
#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.

Django

unread,
Feb 9, 2024, 7:20:33 AMFeb 9
to django-...@googlegroups.com
#35177: Admin.display decorator functions do not output correct-format for 'modern'
Python string-formatting
-------------------------------------+-------------------------------------
Reporter: stephenskett | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 4.2
Severity: Normal | Resolution:
Keywords: admin, display, | Triage Stage:
string, format, | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by stephenskett:

Old description:

New description:

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

[https://docs.python.org/3/library/stdtypes.html#old-string-formatting
'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/library/stdtypes.html#str.format str.format] or
[https://docs.python.org/3/reference/lexical_analysis.html#f-strings


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#comment:1>

Django

unread,
Feb 9, 2024, 12:34:49 PMFeb 9
to django-...@googlegroups.com
#35177: Admin.display decorator functions do not output correct-format for 'modern'
Python string-formatting
-------------------------------------+-------------------------------------
Reporter: Stephen Skett | Owner: nobody
Type: Bug | Status: closed
Component: contrib.admin | Version: 4.2
Severity: Normal | Resolution: invalid

Keywords: admin, display, | Triage Stage:
string, format, | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Baptiste Mispelon):

* status: new => closed
* resolution: => invalid

Comment:

Hi,

The equivalent of `"%.3f" % obj.bar` is `"{0:.3f}".format(obj.bar)` (note
the added `.`).

What you're doing with `"{0:3f}".format(obj.bar)` is making sure the final
string is at least 3 characters long, using the default 6 decimal places
(try replacing the `3` by a `10` for example, you'll see extra spaces at
the beginning of the string).

So Django is not doing anything weird here.
--
Ticket URL: <https://code.djangoproject.com/ticket/35177#comment:2>

Reply all
Reply to author
Forward
0 new messages