如何使用django-rest-framework获取对象级验证错误?

时间:2018-03-21 16:42:07

标签: django django-rest-framework

我们正在将django-rest-framework用于包含对象列表的表单。所有这些都有值,必须在对象级别进行验证。

这是POST,发送3个步骤的列表

"steps":[  
   {  
      "start_date":"2016-03-19",
      ...
   },
   {  
      "start_date":"2018-06-19",
      ...

   },
   {  
      "start_date":"2018-06-19",
      ...
   }
]

以非特定的验证错误回答

{  
   "steps":[  
      {  
         "start_date":[  
            "The start date must be in the future." // Which one? Step 1, 2 or 3?
         ]
      },

   ]
}

串行器:

class StepSerializer(serializers.ModelSerializer):
    name = serializers.CharField(required=True)
    position = serializers.IntegerField(required=True)
    // start_date comes from the Model

class ProjectSerializer(serializers.ModelSerializer):
    name = ...
    steps = StepSerializer()

查看:

class ProjectViewSet(ModelViewSet):
    serializer_class = ProjectSerializer

问题:答案如何包含有关该对象的信息,该信息有错误?

感谢您提供任何帮助!

祝你好运 延

0 个答案:

没有答案