Skip to content

Commit

Permalink
Fix get_prep_value to raise ValidationError instead of ValueError
Browse files Browse the repository at this point in the history
  • Loading branch information
gsakkis committed Aug 9, 2020
1 parent 0d0cc8e commit 2b29b5d
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 14 deletions.
14 changes: 1 addition & 13 deletions enumfields/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def get_prep_value(self, value):
return None
if isinstance(value, self.enum): # Already the correct type -- fast path
return value.value
return self.enum(value).value
return self.to_python(value).value

def from_db_value(self, value, expression, connection, *args):
return self.to_python(value)
Expand Down Expand Up @@ -154,15 +154,3 @@ def validators(self):
# connection.ops.integer_field_range method.
next = super(models.IntegerField, self)
return next.validators

def get_prep_value(self, value):
if value is None:
return None

if isinstance(value, Enum):
return value.value

try:
return int(value)
except ValueError:
return self.to_python(value).value
3 changes: 2 additions & 1 deletion tests/test_django_models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.core.exceptions import ValidationError
from django.db import connection

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

with pytest.raises(ValueError):
with pytest.raises(ValidationError):
MyModel.objects.filter(color='xx')[0]


Expand Down

0 comments on commit 2b29b5d

Please sign in to comment.