Opened 6 days ago

Closed 6 days ago

#35821 closed Bug (invalid)

Integer fields silently round floats

Reported by: Alex Krupp Owned by:
Component: Uncategorized Version: 5.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I encountered a bug in my code today where I was trying to save 0.3 to a PositiveSmallIntegerField with an initial default value of zero, and the model kept showing zero even after saving it. This was super confusing because it looked like the model just wasn't saving, until I eventually realized that the value was being rounded.

Suggested improvements would be either:

1) Raise an error when attempting to save a non-Integer value
2) Update the documentation to specify that floats/decimals are rounded to the nearest integer

Change History (1)

comment:1 by Jacob Walls, 6 days ago

Resolution: invalid
Status: newclosed

Thanks for the ticket, but it sounds like FloatField may be more appropriate for your use case. IntegerField is documented as representing integers, and since int and float are distinct Python types, and throughout the documentation is the theme that Django models coerce inputs to proper python types (sometimes implicitly, elsewhere explicitly in the discussion of to_python), I think this behavior is sufficiently documented. Changing the underlying behavior would introduce backwards compatibility concerns.

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