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

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

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 <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/0107018d8dcfe671-12ce01fd-fb84-4fa3-9c44-dc451a3b8721-000000%40eu-central-1.amazonses.com.

Reply via email to