#23473: Django prompts to create new migrations when none are needed when a 
class
is used in a field declaration
----------------------------+---------------------------------------
     Reporter:  dlo         |                    Owner:  Markush2010
         Type:  Bug         |                   Status:  assigned
    Component:  Migrations  |                  Version:  1.7
     Severity:  Normal      |               Resolution:
     Keywords:              |             Triage Stage:  Accepted
    Has patch:  0           |      Needs documentation:  0
  Needs tests:  0           |  Patch needs improvement:  0
Easy pickings:  0           |                    UI/UX:  0
----------------------------+---------------------------------------

Comment (by Markush2010):

 Replying to [comment:6 dlo]:
 > Is there any way we could also make the `./manage.py makemigrations`
 command fail unless `__eq__` is implemented? Otherwise, users might not
 know where to look to fix the problem.
 This is rather complicated as `object` itself has a `__eq__` method. and
 would require some crazy comparisons. For what I've tested, this seems to
 work.

 Two solutions that somehow work, but that I'm not really a friend of. The
 if clause is really error-prone.

 1. Fails even on `manage.py shell`:

 {{{#!python
 diff --git a/django/utils/deconstruct.py b/django/utils/deconstruct.py
 index a51a169..b0443ee 100644
 --- a/django/utils/deconstruct.py
 +++ b/django/utils/deconstruct.py
 @@ -49,6 +49,13 @@ def deconstructible(*args, **kwargs):
          klass.__new__ = staticmethod(__new__)
          klass.deconstruct = deconstruct

 +        if not hasattr(klass, '__eq__') or klass.__eq__ == object.__eq__:
 +            raise ValueError(
 +                "Cannot deconstruct class %s since no __eq__ method
 exists or "
 +                "__eq__ is inherited from 'object'. This method is
 required "
 +                "to prevent infinit migration creation"
 +                % klass.__name__)
 +
          return klass

      if not args:
 }}}

 2. Fails only when `__eq__` is called:

 {{{#!python
 diff --git a/django/utils/deconstruct.py b/django/utils/deconstruct.py
 index a51a169..7c3abac 100644
 --- a/django/utils/deconstruct.py
 +++ b/django/utils/deconstruct.py
 @@ -49,6 +49,16 @@ def deconstructible(*args, **kwargs):
          klass.__new__ = staticmethod(__new__)
          klass.deconstruct = deconstruct

 +        if not hasattr(klass, '__eq__') or klass.__eq__ == object.__eq__:
 +            def __eq__(obj, other):
 +                raise ValueError(
 +                    "Cannot deconstruct class %s since no __eq__ method "
 +                    "exists or __eq__ is inherited from 'object'. This
 method "
 +                    "is required to prevent infinit migration creation"
 +                    % obj.__class__.__name__)
 +
 +            klass.__eq__ = __eq__
 +
          return klass

      if not args:
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23473#comment:7>
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/061.b2061b6ab4481402f45e67eaa9c8c22b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to