Opened 8 years ago

Closed 8 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:

https://github.com/django/django/blob/ecb59cc6579402b68ddfd4499bf30edacf5963be/django/db/migrations/serializer.py#L363

If so, it adds from decimal import Decimal to the beginning of the migrations file:

https://github.com/django/django/blob/ecb59cc6579402b68ddfd4499bf30edacf5963be/django/db/migrations/serializer.py#L87

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 Tim Graham, 8 years ago

Cc: Markus Holtermann added
Triage Stage: UnreviewedAccepted

Maybe it would be better to move the if hasattr(value, 'deconstruct'): condition earlier in the checks?

comment:2 by Markus Holtermann, 8 years ago

Owner: changed from nobody to Markus Holtermann
Status: newassigned

comment:3 by Markus Holtermann, 8 years ago

Has patch: set

Doesn't warrant a backport as it's the same behavior on 1.7.

comment:4 by Markus Holtermann <info@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 3b38308:

Fixed #26555 -- Gave deconstructible objects a higher priority during serialization

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