serializer.py :
% bigArray has dim: [npolv,nz,nsv]
% npolv=68961 > nsv=200 > nz=81
for qq=1:nsv
for j=1:nz
% the output of fun is a vector with dim npolv
bigArray(:,j,qq) = fun();
end
end
views.py :
class ExamTermSerializer(serializers.Serializer):
name = serializers.CharField()
start_date = serializers.DateField()
end_date = serializers.DateField()
course = serializers.IntegerField()
_class = serializers.IntegerField()
def validate(self,data):
if data['start_date'] > data['end_date']:
raise serializers.ValidationError("Start Date Should be smaller")
return data
当我在DRF中搜索对象级别验证时,我发现我已经在序列化程序(即 ###Bunch of code..
_exm,c = ExamTerm.objects.get_or_create(name = data['name'],
defaults = {
'start_date':data['start_date'],
'end_date':data['end_date'],
'course_id':data['course'],
'_class_id':data['_class']
})
if not c:
raise serializers.ValidationError({
"Detail":['Exam With This name Already Exist']
})
return Response(data , status=status.HTTP_201_CREATED)
)中编写了该验证功能。
我的实际问题是如何从视图中实现此validate
函数,以使validate
始终小于start_date
。
答案 0 :(得分:0)
您需要覆盖save()
模型的ExamTerm
方法
def save(self,*args,**kwargs):
if not self.start_date > self.end_date:
super(ExamTerm,self).save(*args,**kwargs)
else:
raise ValidationError('start date can not be greater than end date')