"applicant": ["Это поле не может быть null."].

serializers.py

views.py from rest_framework import status from rest_framework.response import Response from rest_framework.views import APIView from .serializers import VerificationMobileSerializer, FieldUserSerializer, ApplicantSubmissionSerializer из django.utils.decorators import method_decorator из django.views.decorators.csrf import csrf_exempt from django.shortcuts import get_object_or_404 from .models import FieldUser from rest_framework import generics

@method_decorator(csrf_exempt, name='dispatch')
class VerifyMobileAPIView(APIView):
    def post(self, request):
        serializer = VerificationMobileSerializer(data=request.data)
        if serializer.is_valid():
            mobilenumber = serializer.validated_data['mobile_number']
            queryset = FieldUser.objects.filter(mobile_number=mobilenumber)
            if queryset.exists():
                user_serializer = FieldUserSerializer(queryset, many=True)
                return Response({
                    'detail': 'Verification successful. Login successful.',
                    'users': user_serializer.data
                }, status=status.HTTP_200_OK)
            return Response({'detail': 'Invalid mobile number'}, status=status.HTTP_400_BAD_REQUEST)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


class GetUserProfileAPIView(APIView):
    def get(self, request, pk=None):
        if pk is not None:
            user = get_object_or_404(FieldUser, pk=pk)
            serializer = FieldUserSerializer(user)
            return Response(serializer.data)
        else:
            user = request.user
            serializer = FieldUserSerializer(user)
            return Response(serializer.data)


@method_decorator(csrf_exempt, name='dispatch')
class UserProfileUpdateAPIView(generics.UpdateAPIView):
    queryset = FieldUser.objects.all()
    serializer_class = FieldUserSerializer


@method_decorator(csrf_exempt, name='dispatch')
class ApplicantSubmissionView(APIView):
    def post(self, request):
        serializer = ApplicantSubmissionSerializer(data=request.data)
        print('hello1')
        if serializer.is_valid():
            print('hello2')
            # Save the validated data
            serializer.save()

            # Return a success response
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        # If the data is not valid, return the errors
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

urls.py

когда я пытаюсь

http://127.0.0.1:8000/api/submit-applicant/

с приведенным ниже json

{
    "applicant": {
        "name": "John Doe",
        "contact_no": "1234567890",
        "aadhar_no": "1234 5678 9012",
        "pan_no": "ABCDE1234F",
        "present_address": "123 Main Street, City",
        "permanent_address": "456 Elm Street, City",
        "residence_status": "Owned",
        "ownership_proof_type": "E-Bill/Water Tax",
        "duration_to_stay": "5 years",
        "landmark": "Near the park",
        "area_of_house": "1500 sq. ft.",
        "color_of_house": "Beige",
        "no_of_persons_living_in_home": 4,
        "no_of_earning_members": 2,
        "neighbor_name": "Smith Family",
        "met_person_at_home": "Yes",
        "office_business_name": "ABC Enterprises",
        "office_business_address": "789 Business Avenue, City",
        "duration_of_service_business": "10 years",
        "designation_of_applicant": "Manager",
        "gstin_or_id_card": "GST1234567",
        "nature_of_business": "Retail",
        "staff_name_and_designation": "John Smith, Sales Executive",
        "no_of_employees": 10,
        "contact_no_of_business": 9812345678,
        "landmark_of_business": "Near the mall",
        "ownership_proof_type_of_business": "E-Bill/Water Tax"
        
    },
    "coApplicants": [
        {
            "name": "Jane Doe",
            "contact_no": "9876543210",
            "aadhar_no": "5678 9012 3456",
            "pan_no": "FGHIJ5678K",
            "present_address": "789 Oak Street, City",
            "permanent_address": "901 Pine Street, City",
            "residence_status": "Rented",
            "ownership_proof_type": "E-Bill/Water Tax",
            "duration_to_stay": "3 years",
            "landmark": "Near the school",
            "area_of_house": "1200 sq. ft.",
            "color_of_house": "Blue",
            "no_of_persons_living_in_home": 3,
            "no_of_earning_members": 1,
            "neighbor_name": "Johnson Family",
            "met_person_at_home": "No"
            
        }
    ],
    "granters": [
        {
            "name": "James Smith",
            "contact_no": "2345678901",
            "aadhar_no": "9012 3456 7890",
            "pan_no": "KLMNO6789P",
            "present_address": "345 Maple Street, City",
            "permanent_address": "567 Cedar Street, City",
            "residence_status": "Owned",
            "ownership_proof_type": "E-Bill/Water Tax",
            "duration_to_stay": "7 years",
            "landmark": "Near the hospital",
            "area_of_house": "1800 sq. ft.",
            "color_of_house": "White",
            "no_of_persons_living_in_home": 5,
            "no_of_earning_members": 2,
            "neighbor_name": "Doe Family",
            "met_person_at_home": "Yes"
        }
    ]
}
getting the error

{
    "coApplicants": [
        {
            "applicant": [
                "This field may not be null."
            ]
        }
    ]
} 

и мой кандидат не создается, поэтому id равен null

решение вышеуказанной ошибки

Вернуться на верх