当我输入数据时,我想通过 Django rest API JSON 序列化字段检查数据键是否有效。我在 serializers.py 中有一个 JSON 序列化程序,如
class EmployBasicInfoSerializers(serializers.Serializer):
basic_info = serializers.JSONField()
designation = serializers.JSONField()
academic_info = serializers.JSONField()
address = serializers.JSONField()
password = serializers.JSONField()
我的意见功能是:
@api_view(['POST'])
def employ_create(request):
if request.method == 'POST':
json = JSONRenderer().render(request.data)
stream = io.BytesIO(json)
data = JSONParser().parse(stream)
data_serializer = EmployBasicInfoSerializers(data=data)
if data_serializer.is_valid():
pass
return Response({'data_status':data_serializer.errors}, status=rest_framework.status.HTTP_400_BAD_REQUEST)
然后我输入 JSON 数据,如下所示:
{
"basic_info": {
"name":"tushar",
"gender":"male",
"date_of_birth":"2020-08-22",
"age":27,
"phone_number":8667,
"email":"mntushar25@gmail.com"
},
"designation":{
"id": 2
},
"academic_info":[
{
"degree":"hsc",
"last_passing_institution_name":"hgygjhgy",
"last_passing_year":"2020-08-22"
},
{
"degree":"hsc",
"last_passing_institution_name":"hgygjhgy",
"last_passing_year":"2020-08-22"
}
],
"address":{
"house_no":8787,
"village_name":"kushtia",
"post_office":"daulatpur",
"thana_name":"daulatpur",
"district_name":"kushtia"
},
"password":"admin"
}
但是我从data_serializer验证中得到错误。错误是:
{
"data_status": {
"name": [
"This field is required."
],
"gender": [
"This field is required."
],
"date_of_birth": [
"This field is required."
],
"phone_number": [
"This field is required."
],
"email": [
"This field is required."
]
}
}
我不明白为什么会发生这些错误。请帮我解决错误.....