Git Product home page Git Product logo

Comments (6)

samh avatar samh commented on July 17, 2024

Fixed by 7b4c20b

from django-enumfields.

bh avatar bh commented on July 17, 2024

Does not work:

class FooModel(modelsModels):
    class Gender(Enum):
        MALE = "m"
        FEMALE = "f"
        UNKNOWN = "u"

        class Labels:
            MALE = 'Male',
            FEMALE = 'Female',
            UNKNOWN = 'Unknown'

    gender = EnumField(Gender, max_length=1, blank=True, null=True)

python manage.py makemigrations returns:

  File "/home/bhe/.virtualenvs/mx-py34/lib/python3.4/site-packages/django/db/migrations/writer.py", line 349, in serialize
    raise ValueError("Cannot serialize: %r\nThere are some values Django cannot serialize into migration files.\nFor more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing" % value)
ValueError: Cannot serialize: <Gender.MALE: 'm'>
There are some values Django cannot serialize into migration files.
For more, see https://docs.djangoproject.com/en/dev/topics/migrations/#migration-serializing

from django-enumfields.

samh avatar samh commented on July 17, 2024

I see; I hadn't actually tried migrations (just got it to work with unmigrated models).

I was able to get it working by adding a deconstruct method in the init method like so:

def __init__(self, value):
    def deconstruct():
        return (
            '%s.%s' % (self.__class__.__module__, self.__class__.__name__),
            (self.value,),
            {}
        )
    self.deconstruct = deconstruct

A standard deconstruct method fails because Django tries to call it on the type, not just the instances. I think this might be considered a Django bug, so I haven't yet made a pull request here. If I've missed something, I'm open to suggestions.

I would also remove 'choices' in the deconstruct method in EnumFieldMixin, since it should be generated from the enum:

kwargs.pop('choices', None)

from django-enumfields.

bh avatar bh commented on July 17, 2024

There could occur another issue with nested classes.

class FooModel(models.Model):
    class Gender(Enum):
        MALE = "m"
        FEMALE = "f"
        UNKNOWN = "u"
    g = EnumFIeld(Gender)

vs.

class Gender(Enum):
    MALE = "m"
    FEMALE = "f"
    UNKNOWN = "u"

class FooModel(models.Model):
    g = EnumFIeld(Gender)

Something like (self.__class__.__module__, self.__class__.__name__), cannot be used with nested classes, I think. Because the Django migration engine cannot import a class from another one.

from django-enumfields.

samh avatar samh commented on July 17, 2024

Related Django bug: https://code.djangoproject.com/ticket/22951

from django-enumfields.

matthewwithanm avatar matthewwithanm commented on July 17, 2024

@bh @samh Sorry this went under the radar ):

I just pushed 0.5.1 which removes choices from the deconstructed kwargs. I think we can get around the nested class issue by improving our handling of the string case and having deconstruct() always use a string.

from django-enumfields.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.