#26555: makemigrations serializer isinstance() check fails for subclasses of
Decimal (and DateTime etc)
----------------------------+--------------------
     Reporter:  oTree-org   |      Owner:  nobody
         Type:  Bug         |     Status:  new
    Component:  Migrations  |    Version:  master
     Severity:  Normal      |   Keywords:
 Triage Stage:  Unreviewed  |  Has patch:  0
Easy pickings:  0           |      UI/UX:  0
----------------------------+--------------------
 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`.

--
Ticket URL: <https://code.djangoproject.com/ticket/26555>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.bae500896bff1a3a163bd27abb6ab2fa%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to