Skip to content

Raise ValidationError in Django fields #159

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions semantic_version/django_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import django
from django.db import models
from django.core.exceptions import ValidationError

if django.VERSION >= (3, 0):
# See https://docs.djangoproject.com/en/dev/releases/3.0/#features-deprecated-in-3-0
Expand Down Expand Up @@ -75,10 +76,13 @@ def to_python(self, value):
return value
if isinstance(value, base.Version):
return value
if self.coerce:
return base.Version.coerce(value, partial=self.partial)
else:
return base.Version(value, partial=self.partial)
try:
if self.coerce:
return base.Version.coerce(value, partial=self.partial)
else:
return base.Version(value, partial=self.partial)
except ValueError as e:
raise ValidationError(self.default_error_messages['invalid'], code='invalid') from e


class SpecField(SemVerField):
Expand All @@ -104,4 +108,7 @@ def to_python(self, value):
return value
if isinstance(value, base.BaseSpec):
return value
return base.BaseSpec.parse(value, syntax=self.syntax)
try:
return base.BaseSpec.parse(value, syntax=self.syntax)
except ValueError as e:
raise ValidationError(self.default_error_messages['invalid'], code='invalid') from e