check rancher unique national code in serilaizer validation
This commit is contained in:
7
apps/herd/exception.py
Normal file
7
apps/herd/exception.py
Normal file
@@ -0,0 +1,7 @@
|
||||
from rest_framework.exceptions import APIException
|
||||
from rest_framework import status
|
||||
|
||||
|
||||
class UniqueRancherApiException(APIException):
|
||||
status_code = status.HTTP_403_FORBIDDEN
|
||||
default_detail = "دامدار با این کد ملی از قبل در سیستم وجود دارد" # noqa
|
||||
@@ -4,6 +4,7 @@ from apps.authentication.api.v1.serializers.serializer import (
|
||||
ProvinceSerializer,
|
||||
CitySerializer
|
||||
)
|
||||
from apps.herd.exception import UniqueRancherApiException
|
||||
from rest_framework import serializers
|
||||
from apps.herd.models import Herd, Rancher
|
||||
|
||||
@@ -35,6 +36,12 @@ class RancherSerializer(serializers.ModelSerializer):
|
||||
model = Rancher
|
||||
fields = '__all__'
|
||||
|
||||
def validate(self, attrs):
|
||||
""" some validations for operations relate to rancher """
|
||||
|
||||
if self.Meta.model.objects.filter(national_code=attrs['national_code']).exists():
|
||||
raise UniqueRancherApiException()
|
||||
|
||||
def to_representation(self, instance):
|
||||
""" customize output of serializer """
|
||||
|
||||
|
||||
Reference in New Issue
Block a user