Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Django 1.7b4 #11

Closed
bh opened this issue Jun 11, 2014 · 6 comments
Closed

Support for Django 1.7b4 #11

bh opened this issue Jun 11, 2014 · 6 comments

Comments

@bh
Copy link

bh commented Jun 11, 2014

Hi,
could you please add support for Django >= 1.7. I get the following error:

TypeError: Couldn't reconstruct field gender on webapp.MxUser: __init__() missing 1 required positional argument: 'enum'

.. when running:

$ python manage.py makemigrations

Thank you!

@samh
Copy link
Contributor

samh commented Jul 1, 2014

Fixed by 7b4c20b

@bh
Copy link
Author

bh commented Jul 1, 2014

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

@samh
Copy link
Contributor

samh commented Jul 3, 2014

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)

@bh
Copy link
Author

bh commented Jul 4, 2014

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.

@samh
Copy link
Contributor

samh commented Aug 20, 2014

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

@matthewwithanm
Copy link
Contributor

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants