1

django 1.10, py 3.5

Here's my Enum-like class:

@deconstructible
class EnumType(object):
    @classmethod
    def choices(cls):
        attrs = [i for i in cls.__dict__.keys() if i[:1] != '_' and i.isupper()]
        return tuple((cls.__dict__[attr], cls.__dict__[attr]) for attr in attrs)

    def __eq__(self, other):
        return self.choices() == other.choices()

Here's an example of the class:

class TransmissionType(EnumType):
    TRANSMISSION_PROGRAM = 'TRANSMISSION_PROGRAM'
    INFO_PROGRAM = 'INFO_PROGRAM'
    SPORT_PROGRAM = 'SPORT_PROGRAM'

Here's how i use it on a model:

type = models.TextField(choices=TransmissionType.choices(), db_index=True, default=None)

I think I made everything right according to the current django deconstruct docs but apparently makemigration script still creates everytime migration like this:

operations = [
    migrations.AlterField(
        model_name='transmission',
        name='type',
        field=models.TextField(choices=[('TRANSMISSION_PROGRAM', 'TRANSMISSION_PROGRAM'), ('INFO_PROGRAM', 'INFO_PROGRAM'), ('SPORT_PROGRAM', 'SPORT_PROGRAM')], db_index=True, default=None),
    ),
]

Edit1: expected behaviour - when class members does not change the generated migaration should not include AlterField

jnowak
  • 374
  • 1
  • 2
  • 12

1 Answers1

2

Dictionaries have an arbitrary order, so your tuple has an arbitrary order as well. Especially on Python 3.3+ the order is likely to change because it uses a random hash seed. As such, the order of the tuple is different as well, and tuples with the same items but a different order don't compare equal. Django detects this change and creates a new migration.

To fix this, simply sort the keys before constructing the tuple:

@deconstructible
class EnumType(object):
    @classmethod
    def choices(cls):
        attrs = [i for i in cls.__dict__.keys() if i[:1] != '_' and i.isupper()]
        return tuple((cls.__dict__[attr], cls.__dict__[attr]) for attr in sorted(attrs))

    def __eq__(self, other):
        return self.choices() == other.choices()
Community
  • 1
  • 1
knbk
  • 46,344
  • 6
  • 103
  • 109