diff --git a/apis_core/generic/forms/fields.py b/apis_core/generic/forms/fields.py index 60da3a635..7b31e87f6 100644 --- a/apis_core/generic/forms/fields.py +++ b/apis_core/generic/forms/fields.py @@ -1,10 +1,17 @@ +from django.core.exceptions import ValidationError from django.forms import ModelChoiceField +from django.utils.translation import gettext as _ from apis_core.utils.helpers import create_object_from_uri class ModelImportChoiceField(ModelChoiceField): def to_python(self, value): - result = create_object_from_uri(value, self.queryset.model) - if result is not None: - return result - return super().to_python(value) + result = None + try: + result = create_object_from_uri(value, self.queryset.model) + except Exception as e: + raise ValidationError( + _("Could not import %(value)s: %(exception)s"), + params={"value": value, "exception": e}, + ) + return result or super().to_python(value)