-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(generic): let the importer give more meaningfull error messages
- Loading branch information
Showing
1 changed file
with
11 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |