Skip to content

Commit 2b29b5d

Browse files
committed
Fix get_prep_value to raise ValidationError instead of ValueError
1 parent 0d0cc8e commit 2b29b5d

File tree

2 files changed

+3
-14
lines changed

2 files changed

+3
-14
lines changed

enumfields/fields.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def get_prep_value(self, value):
6666
return None
6767
if isinstance(value, self.enum): # Already the correct type -- fast path
6868
return value.value
69-
return self.enum(value).value
69+
return self.to_python(value).value
7070

7171
def from_db_value(self, value, expression, connection, *args):
7272
return self.to_python(value)
@@ -154,15 +154,3 @@ def validators(self):
154154
# connection.ops.integer_field_range method.
155155
next = super(models.IntegerField, self)
156156
return next.validators
157-
158-
def get_prep_value(self, value):
159-
if value is None:
160-
return None
161-
162-
if isinstance(value, Enum):
163-
return value.value
164-
165-
try:
166-
return int(value)
167-
except ValueError:
168-
return self.to_python(value).value

tests/test_django_models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.core.exceptions import ValidationError
12
from django.db import connection
23

34
import pytest
@@ -20,7 +21,7 @@ def test_field_value():
2021
m = MyModel.objects.filter(color='r')[0]
2122
assert m.color == Color.RED
2223

23-
with pytest.raises(ValueError):
24+
with pytest.raises(ValidationError):
2425
MyModel.objects.filter(color='xx')[0]
2526

2627

0 commit comments

Comments
 (0)