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.
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>
* 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>