Opened 8 years ago

Closed 8 years ago

#6501 closed (duplicate)

DecimalField does not correctly set default value

Reported by: Moof Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: UI/UX:

Description

DecimalField's default argument appears to set the field to a unicode rather than a Decimal instance, this then makes calculations involving Decimals not work.

I've attached a small models.py file that can be used to check the behaviour, including expected results and the actual ones.

There's a smallish bug that Decimal("0.00") doesn't retain its precision on loading, but this may be a python bug.

Attachments (2)

force_unicode.patch (800 bytes) - added by Moof 8 years ago.
Fixes force_unicode to take Decimal values into account
models.py (716 bytes) - added by Moof 8 years ago.
Failing tests for Decimal

Download all attachments as: .zip

Change History (5)

comment:1 Changed 8 years ago by Moof

  • Needs documentation unset
  • Needs tests set
  • Patch needs improvement unset

Attached: A patch to fix the strings_only behaviour of django.utils.encoding.force_unicode() which seems to be the cause of the problem, as it checks for certain types and misses out Decimal.

This fixes the default being returned as a unicode rather than a Decimal.

However, I can't seem to find an obvious place in the test suite to add these tests.

Changed 8 years ago by Moof

Fixes force_unicode to take Decimal values into account

Changed 8 years ago by Moof

Failing tests for Decimal

comment:2 Changed 8 years ago by Moof

  • Has patch set

Removed the precision tests. That should be in another ticket, and needs a fuller discussion.
This ticket and patch is merely for the default value problem.

comment:3 Changed 8 years ago by ramiro

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #5903 that included a similar patch and adds a regression test.

Note: See TracTickets for help on using tickets.
Back to Top