mirror of
https://github.com/inventree/InvenTree.git
synced 2025-05-02 05:26:45 +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:
parent
e57a8cdcd1
commit
529beb3d58
@ -2,7 +2,6 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
from rest_framework import generics
|
|
||||||
|
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
@ -22,3 +21,20 @@ class UserSerializerBrief(serializers.ModelSerializer):
|
|||||||
'pk',
|
'pk',
|
||||||
'username',
|
'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
|
||||||
|
@ -235,4 +235,4 @@ bom_api_urls = [
|
|||||||
|
|
||||||
# Catch-all
|
# Catch-all
|
||||||
url(r'^.*$', BomList.as_view(), name='api-bom-list'),
|
url(r'^.*$', BomList.as_view(), name='api-bom-list'),
|
||||||
]
|
]
|
||||||
|
@ -3,6 +3,8 @@ from rest_framework import serializers
|
|||||||
from .models import Part, PartCategory, BomItem
|
from .models import Part, PartCategory, BomItem
|
||||||
from .models import SupplierPart, SupplierPriceBreak
|
from .models import SupplierPart, SupplierPriceBreak
|
||||||
|
|
||||||
|
from InvenTree.serializers import InvenTreeModelSerializer
|
||||||
|
|
||||||
|
|
||||||
class CategorySerializer(serializers.ModelSerializer):
|
class CategorySerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
@ -65,15 +67,10 @@ class PartSerializer(serializers.ModelSerializer):
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class BomItemSerializer(serializers.ModelSerializer):
|
class BomItemSerializer(InvenTreeModelSerializer):
|
||||||
|
|
||||||
# url = serializers.CharField(source='get_absolute_url', read_only=True)
|
# url = serializers.CharField(source='get_absolute_url', read_only=True)
|
||||||
|
|
||||||
def validate(self, data):
|
|
||||||
instance = BomItem(**data)
|
|
||||||
instance.clean()
|
|
||||||
return data
|
|
||||||
|
|
||||||
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
|
part_detail = PartBriefSerializer(source='part', many=False, read_only=True)
|
||||||
sub_part_detail = PartBriefSerializer(source='sub_part', many=False, read_only=True)
|
sub_part_detail = PartBriefSerializer(source='sub_part', many=False, read_only=True)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user