0

As you can see when I print the serializer by print(serializer) statement, the output there is =

AddCompanySerializer(data={'name': 'Nestle', 'est': '1866', 'org': 'Switzerland'}): name = CharField(max_length=30, required=False, validators=[<UniqueValidator(queryset=Company.objects.all())>]) established = IntegerField(required=False) origin = CharField(max_length=64, required=False)

But when I'm trying to access the data by serializer.data its returning only

{'name': 'Nestle'} instead of the whole {'name': 'Nestle', 'est': '1866', 'org': 'Switzerland'}.
  1. React Frontend(sending the post request with the data)
handleOnSubmit = (event)=>{
        const requestOptions = {
            method : "POST",
            headers : {
                Accept: "application/json",
                "Content-Type":"application/json"},
            body : JSON.stringify({
                name:this.state.name,
                est:this.state.est,
                org:this.state.org
            }),
        };
        fetch("api/add-company", requestOptions).then((response)=>{return response.json()}).then((data)=>{
            console.log(data)
        })
        event.preventDefault();
    }
  1. Rest Framework function
@api_view(['POST'])
def add_company(request):
    serializer = AddCompanySerializer(data=request.data)
    if serializer.is_valid():
        print(serializer.data.get('name'))
        print(serializer.data.get('est'))
        print(serializer.data)
        print(serializer)
        return Response(serializer.data, status=status.HTTP_200_OK)
    print(serializer.errors)
    return Response({"Not working"}, status=status.HTTP_400_BAD_REQUEST)

3.Terminal Outputs of the print statements

Nestle

None

{'name': 'Nestle'}
  1. Form Image

Form inputs

You must log in to answer this question.

Browse other questions tagged .