You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
897 B
28 lines
897 B
|
10 months ago
|
from rest_framework import serializers
|
||
|
|
|
||
|
|
from staff.models import Staff
|
||
|
|
from utils.custom import MyCustomError
|
||
|
|
|
||
|
|
|
||
|
|
class StaffSerializer(serializers.ModelSerializer):
|
||
|
|
"""
|
||
|
|
员工序列化
|
||
|
|
"""
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
model = Staff
|
||
|
|
fields = "__all__"
|
||
|
|
|
||
|
|
def validate_idCode(self, value):
|
||
|
|
# 从context中获取companyMid
|
||
|
|
# companyMid = self.context.get('companyMid')
|
||
|
|
# 当修改与创建时,检查idCode是否已存在
|
||
|
|
if self.instance: # Update case
|
||
|
|
existing_staff = Staff.objects.exclude(id=self.instance.id).filter(idCode=value, companyMid=self.initial_data.get('companyMid'))
|
||
|
|
else: # Create case
|
||
|
|
existing_staff = Staff.objects.filter(idCode=value, companyMid=self.initial_data.get('companyMid'))
|
||
|
|
|
||
|
|
if existing_staff.exists():
|
||
|
|
raise MyCustomError("该身份码已存在")
|
||
|
|
|
||
|
|
return value
|