Skip to content

Commit

Permalink
Merge pull request #3 from Alexander3/develop
Browse files Browse the repository at this point in the history
fix choices bug on makemigrations
  • Loading branch information
wolph committed Apr 16, 2017
2 parents 6c90f33 + a4fcc68 commit 45056fc
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions django_utils/choices.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,12 @@ class Enum(choices.Choices):
SomeModel.create(enum=SomeModel.Enum.Spam)
'''
import six
import collections
from django.utils import deconstruct

import six

class ChoicesDict(object):

class ChoicesDict(object):
'''The choices dict is an object that stores a sorted representation of
the values by key and database value'''

Expand Down Expand Up @@ -123,9 +122,7 @@ def __str__(self):
return six.text_type(self._by_key)


@deconstruct.deconstructible
class Choice(object):

'''The choice object has an optional label and value. If the value is not
given an autoincrementing id (starting from 1) will be used
Expand All @@ -150,6 +147,11 @@ def __init__(self, value=None, label=None):
self.label = label
self.order = Choice.order

def __eq__(self, other):
if not isinstance(other, Choice):
return False
return self.value == other.value and self.label == self.label

def __repr__(self):
repr_ = (six.text_type('<%s[%d]:%s>') % (
self.__class__.__name__,
Expand All @@ -176,9 +178,15 @@ def __unicode__(self):
elif six.PY3:
return six.text_type(label)

def deconstruct(self):
return (
'{}.{}'.format(self.__class__.__module__, self.__class__.__name__),
(self.value, self.label),
{},
)

class ChoicesMeta(type):

class ChoicesMeta(type):
'''The choices metaclass is where all the magic happens, this
automatically creates a ChoicesDict to get a sorted list of keys and
values'''
Expand Down Expand Up @@ -213,7 +221,6 @@ def __new__(cls, name, bases, attrs):


class Choices(six.with_metaclass(ChoicesMeta)):

'''The choices class is what you should inherit in your Django models
>>> choices = Choices()
Expand Down

0 comments on commit 45056fc

Please sign in to comment.