Code

#19220 closed Bug (fixed)

Django admin form fields for a models.DecimalField with value 0 or 0.0 show "0E-8"

Reported by: nebstrebor Owned by: antofik
Component: contrib.admin Version: 1.4
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: yes

Description

We use django admin as a client-facing backend, so we need to make it user friendly. I have a model with a bunch of DecimalFields representing nutritional data.

The fields all look like this:

g_carbs = DecimalField(max_digits=13, decimal_places = 8, null=True, blank=True)

If the field is left blank, or if a non-zero value is provided, the admin form looks and works great. For example, for a non-blank, non-zero value like 10.5, it displays something like 10.50000000, which is fine.

The problem is that for any 0 values, the form field displays 0E-8 which, although technically correct, is not going to cut it for clients, who are not scientists or engineers for the most part and are unfamiliar with E notation. We're using DecimalFields rather than FloatFields to avoid this type of notation.

I am not using a custom ModelForm or any custom admin tricks. Its just what gets auto-rendered by django admin for that model.

Attachments (0)

Change History (6)

comment:1 Changed 18 months ago by mrj

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Design decision needed

It looks like you've got a good response from the StackOverflow question:

http://stackoverflow.com/questions/13163167/how-to-get-a-django-admin-form-field-for-a-models-decimalfield-with-value-0-not

I'm marking as design decision needed, since it looks like there are many out there running into the same question. Does it make sense to have a way to normalize values in DecimalFields before displaying?

comment:2 Changed 17 months ago by NateD

I recently encountered this issue, and will implement the linked SO solution.

Has handling of Decimal fields changed in 1.4? I am very surprised that I didn't encounter this problem earlier. I suggest adding an option to the DecimalField class of 'use_scientific', which defaults to false.

comment:3 Changed 13 months ago by aaugustin

  • Triage Stage changed from Design decision needed to Accepted

The answer on SO is off topic. The problem described here is about 0, and 0 is a perfectly accurate value. This is a bug.

comment:4 Changed 13 months ago by antofik

  • Owner changed from nobody to antofik
  • Status changed from new to assigned

comment:5 Changed 13 months ago by antofik

Fixed the bug and added tests. Was forced to create new test folder since there were no tests for /backend/db/util.py
Branch with fixes https://github.com/antofik/django/tree/ticket_19220

comment:6 Changed 13 months ago by Claude Paroz <claude@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 3ff3212713403c5bb8c5b067d2f00d62cf111036:

Fixed #19220 -- Prevented decimals to be displayed in scientific notation

Thanks nebstrebor for the report and antofik for the patch.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.