From b97866e7711c5cc5c8f203b8260ef44852f67819 Mon Sep 17 00:00:00 2001 From: Noah Huppert Date: Thu, 4 Apr 2024 14:34:42 -0400 Subject: [PATCH] made VersionField raise correct Django exception --- semantic_version/django_fields.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/semantic_version/django_fields.py b/semantic_version/django_fields.py index e5bd7eb..8c45bcc 100644 --- a/semantic_version/django_fields.py +++ b/semantic_version/django_fields.py @@ -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 @@ -75,10 +76,14 @@ 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(str(e)) from e class SpecField(SemVerField):