Skip to content

Commit

Permalink
fix(generic): let the importer give more meaningfull error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Apr 2, 2024
1 parent d3eb810 commit f036445
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions apis_core/generic/forms/fields.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit f036445

Please sign in to comment.