1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #1285: Enforce model validation when creating/editing objects via the API

This commit is contained in:
Jeremy Stretch
2017-07-06 17:37:24 -04:00
parent 5b43a108bc
commit 1f9806a480
8 changed files with 74 additions and 31 deletions

View File

@ -98,6 +98,17 @@ class ContentTypeFieldSerializer(Field):
raise ValidationError("Invalid content type")
class ModelValidationMixin(object):
"""
Enforce a model's validation through clean() when validating serializer data. This is necessary to ensure we're
employing the same validation logic via both forms and the API.
"""
def validate(self, attrs):
instance = self.Meta.model(**attrs)
instance.clean()
return attrs
class WritableSerializerMixin(object):
"""
Allow for the use of an alternate, writable serializer class for write operations (e.g. POST, PUT).