Opened 9 years ago
Closed 9 years ago
#26555 closed Bug (fixed)
makemigrations serializer isinstance() check fails for subclasses of Decimal (and DateTime etc)
Reported by: | oTree-org | Owned by: | Markus Holtermann |
---|---|---|---|
Component: | Migrations | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Markus Holtermann | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
The migrations serializer uses isinstance(value, decimal.Decimal)
to check if a value is a decimal:
If so, it adds from decimal import Decimal
to the beginning of the migrations file:
The problem with isinstance()
is that it results in false positives for subclasses. I am using a subclass of Decimal
called Money
as the default value for many of my model fields (subclass of DecimalField
called MoneyField
), and the migrations writer adds an incorrect from decimal import Decimal
to my migrations files, which still results in a NameError
for Money
when I run migrate
.
This problem also prevents me from using the deconstruct()
method as recommended in the documentation to serialize custom classes:
https://docs.djangoproject.com/ja/1.9/topics/migrations/#adding-a-deconstruct-method
(Because the check for Decimal
happens before the check for .deconstruct()
.)
I think instead of isinstance(value, decimal.Decimal)
, the code should be if type(value) == decimal.Decimal
.
Change History (4)
comment:1 by , 9 years ago
Cc: | added |
---|---|
Triage Stage: | Unreviewed → Accepted |
comment:2 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 9 years ago
Has patch: | set |
---|
Doesn't warrant a backport as it's the same behavior on 1.7.
Maybe it would be better to move the
if hasattr(value, 'deconstruct'):
condition earlier in the checks?