JSON fields serializer errors in Django rest framework

392 views Asked by At

when I do input data then I want to check the data key is valid or not by Django rest API JSON serialize field. I have a JSON serializer in serializers.py like as

class EmployBasicInfoSerializers(serializers.Serializer):
    basic_info = serializers.JSONField()
    designation = serializers.JSONField()
    academic_info = serializers.JSONField()
    address = serializers.JSONField()
    password = serializers.JSONField()

and my views function is:

@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)

Then I input JSON data like as:

{
    "basic_info": {
        "name":"tushar",
        "gender":"male",
        "date_of_birth":"2020-08-22",
        "age":27,
        "phone_number":8667,
        "email":"[email protected]"
    },
    "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"
}

But I get errors form data_serializer validation. The errors are:

{
    "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."
        ]
    }
}

I can't understand why these errors occur. pls, help me to solve the errors.....

0

There are 0 answers