Error when ContentType is in wrong format

When spliting the ContentType returns only one item, assignating the
app_label and model fails. The API returns a 500 error.

Ensure that a ValidationError is returned if a ValueError is raised
during the assignation
This commit is contained in:
Anthony Ruhier 2019-03-06 15:47:39 +01:00
parent 0c142f2078
commit 66d1e88f5a
No known key found for this signature in database
GPG Key ID: 73E9C8657EC59E3A

View File

@ -109,10 +109,10 @@ class ContentTypeField(Field):
return "{}.{}".format(obj.app_label, obj.model)
def to_internal_value(self, data):
app_label, model = data.split('.')
try:
app_label, model = data.split('.')
return ContentType.objects.get_by_natural_key(app_label=app_label, model=model)
except ContentType.DoesNotExist:
except (ContentType.DoesNotExist, ValueError):
raise ValidationError("Invalid content type")