Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
Django REST API accept list instead of dictionary in post request

I am trying to consume data from a callback API that sends the POST request in this format:

[
  {
    "key1": "asd",
    "key2": "123"
  }
]

However my API currently only works when it is sent like this:

{
  "key1": "asd",
  "key2": "123"
}

serializers.py:

class RawIncomingDataSerializer(serializers.ModelSerializer):
    class Meta:
        model = RawIncomingData
        fields = '__all__'

views.py:

class RawIncomingDataViewSet(viewsets.ModelViewSet):
    queryset = RawIncomingData.objects.all()
    serializer_class = RawIncomingDataSerializer

There will only ever be one object in the post data, so I am looking for a simple work around without having to rewrite my serializer to interpret multiple objects in one post request.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

In that case you can override create and explicitly specify many=True in the get_serializer call:

class RawIncomingDataViewSet(viewsets.ModelViewSet):
    ...
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data, many=True)
        serializer.is_valid(raise_exception=True)
        self.perform_create(serializer)
        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
over 4 years ago · Santiago Trujillo Report

0

The idea is to pass many=True into the serializer class. So, I would choose to override the get_serializer(...) method, as

class RawIncomingDataViewSet(viewsets.ModelViewSet):
    queryset = RawIncomingData.objects.all()
    serializer_class = RawIncomingDataSerializer

    def get_serializer(self, *args, **kwargs):
        if self.action == "create":
            kwargs["many"] = True
        return super().get_serializer(*args, **kwargs)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!