mirror of
https://github.com/inventree/InvenTree.git
synced 2025-07-16 17:56:30 +00:00
Abstract DRF serializer validation
- Subclass InvenTree.InvenTreeModelSerializer - Ensures model data is checked too (after serializer checks are performed)
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from rest_framework import serializers
|
||||
from rest_framework import generics
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
@@ -22,3 +21,20 @@ class UserSerializerBrief(serializers.ModelSerializer):
|
||||
'pk',
|
||||
'username',
|
||||
]
|
||||
|
||||
|
||||
class InvenTreeModelSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
Inherits the standard Django ModelSerializer class,
|
||||
but also ensures that the underlying model class data are checked on validation.
|
||||
"""
|
||||
|
||||
def validate(self, data):
|
||||
# Run any native validation checks first (may throw an ValidationError)
|
||||
data = super(serializers.ModelSerializer, self).validate(data)
|
||||
|
||||
# Now ensure the underlying model is correct
|
||||
instance = self.Meta.model(**data)
|
||||
instance.clean()
|
||||
|
||||
return data
|
||||
|
Reference in New Issue
Block a user