diff --git a/django_enum_choices/fields.py b/django_enum_choices/fields.py index 37acaf0..4610c75 100644 --- a/django_enum_choices/fields.py +++ b/django_enum_choices/fields.py @@ -139,12 +139,24 @@ def validate(self, value, *args, **kwargs): if not self.blank and value in self.empty_values: raise ValidationError(self.error_messages['blank'], code='blank') - if value not in self.enum_class: - raise ValidationError( - self.error_messages['invalid_choice'], - code='invalid_choice', - params={'value': value} - ) + enum_value = value + + if not isinstance(enum_value, self.enum_class): + try: + enum_value = self.to_enum_value(value) + + if enum_value not in self.enum_class: + raise ValidationError( + self.error_messages['invalid_choice'], + code='invalid_choice', + params={'value': value} + ) + except ValidationError: + raise ValidationError( + self.error_messages['invalid_choice'], + code='invalid_choice', + params={'value': value} + ) def value_to_string(self, obj): value = self.value_from_object(obj) diff --git a/django_enum_choices/forms.py b/django_enum_choices/forms.py index 729503b..1b018cd 100644 --- a/django_enum_choices/forms.py +++ b/django_enum_choices/forms.py @@ -42,4 +42,4 @@ def prepare_value(self, value): return value def valid_value(self, value): - return value in self.enum_class + return isinstance(value, self.enum_class) and value in self.enum_class diff --git a/setup.cfg b/setup.cfg index 37617a4..7a6a06e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,5 +6,4 @@ commit = True [flake8] max-line-length = 120 -exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules,*/settings/*,.venv,*/e2e/e2e/* - +exclude = .tox,.git,*/migrations/*,*/static/CACHE/*,docs,node_modules,*/settings/*,.venv,*/e2e/* diff --git a/tox.ini b/tox.ini index 9e410ba..536ebac 100644 --- a/tox.ini +++ b/tox.ini @@ -1,13 +1,17 @@ [tox] envlist = - lint-py{37} - django22-py{37,36} - django21-py{37,36,35} - django111-py{37,36,35} + lint-py{37,38} + django31-py{38,37,36} + django30-py{38,37,36} + django22-py{38,37,36} + django21-py{38,37,36,35} + django111-py{38,37,36,35} [testenv] deps = {[base]deps} + django31: {[django]3.1} + django30: {[django]3.0} django22: {[django]2.2} django21: {[django]2.1} django111: {[django]1.11} @@ -20,6 +24,11 @@ deps = flake8 commands = flake8 django_enum_choices/ +[testenv:lint-py38] +deps = + flake8 +commands = flake8 django_enum_choices/ + [base] deps = pytest @@ -29,8 +38,16 @@ deps = psycopg2 [django] +3.1 = + Django>=3.1.0,<3.2.0 + djangorestframework>=3.7.3 + django-filter>=2.2.0 +3.0 = + Django>=3.0.0,<3.1.0 + djangorestframework>=3.7.3 + django-filter>=2.2.0 2.2 = - Django>=2.2.0,<2.3.0 + Django>=2.2.0,<2.2.17 djangorestframework>=3.7.3 django-filter>=2.2.0 2.1 = @@ -39,5 +56,5 @@ deps = django-filter>=2.2.0 1.11 = Django>=1.11.0,<2.0.0 - djangorestframework>=3.6.2 - django-filter>=2.2.0 \ No newline at end of file + djangorestframework>=3.6.2,<3.9.0 + django-filter>=2.2.0