serializer中的验证器

class ExpertCreateSerializer(serializers.ModelSerializer):
createTime = serializers.HiddenField(default=datetime.now)
createUser = serializers.HiddenField(default=serializers.CurrentUserDefault())

def create(self, validated_data):
    expert = validated_data["expert"]
    expertGroup = validated_data["expertGroup"]
    if expert and expertGroup:
        instance = Expert.objects.create(**validated_data)
        return instance
    raise serializers.ValidationError("专家组或用户信息错误")

class Meta:
    model = Expert
    exclude = ('modifyUser', 'modifyTime')
    validators = [
        UniqueTogetherValidator(
            queryset=Expert.objects.all(),
            fields=('expert', 'expertGroup'),
            message="用户已经添加此专家组,请不要重复添加"
        )
    ]

你可能感兴趣的:(serializer中的验证器)