Opened 12 years ago

Closed 12 years ago

#18613 closed Uncategorized (invalid)

DecimalField returns an int in some circumstances

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

Description

model, _ = MyModel.objects.get_or_create(my_decimal=10)
model.my_decimal.__class__
>>> <type 'int'>
model = MyModel.objects.all()[0]
model.my_decimal.__class__
>>> <class 'decimal.Decimal'>

not sure if this is a bug but it's definetly a gotcha.

Change History (1)

comment:1 by anonymous, 12 years ago

Component: FormsDatabase layer (models, ORM)
Resolution: invalid
Status: newclosed

If you want the object returned from get_or_create to have a decimal.Decimal value for the my_decimal field, then pass a decimal.Decimal value in on the get_or_create call. The fact that Django will accept other types here, and still allow the model to be created successfully and correctly, may be somewhat surprising but can't at this point be changed without breaking backwards-compatibility. Even coercing the field values to match expected types would have backwards-compatible implications. Django takes what's provided, if that's close enough to what's needed for the field type things work, but Django doesn't enforce strict type checking on this (or any?) field, nor does it "fixup" the field values in the returned model object. The ticket description doesn't provide any guidance on what's wanted for a fix here, so I'm not sure what to do other than close this as invalid since this is longstanding behavior that cannot reasonably be changed now.

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