Code

Opened 2 years ago

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

Attachments (0)

Change History (1)

comment:1 Changed 2 years ago by anonymous

  • Component changed from Forms to Database layer (models, ORM)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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.

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.