0

Unfortunately I'm facing the same problem as mentioned in this post. But it didn't solve the problem in my case.

My model:

class AppointmentCategory(models.Model):
    name = models.CharField(max_length=100, default='General')

My Serializer:

class AppointmentCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = AppointmentCategory
        fields = ('id', 'name',)

My View:

class AppointmentCategoryViewSet(viewsets.ModelViewSet):
    queryset = AppointmentCategory.objects.all()
    serializer_class = AppointmentCategorySerializer(many=True)

I'm passing the post data in this format:

 [
  {
    "name": "Emergency"
  },
  {
    "name": "General"   
  }
 ]

It does work when I send only one element. However the above mentioned list fails to create two objects in the database. The error says:

'ListSerializer' object is not callable

I'm not sure how to solve this. Any ideas?

QuestionEverything
  • 4,809
  • 7
  • 42
  • 61

1 Answers1

1

You shouldn't be instantiating the serialiser in the viewset definition. Just do:

serializer_class = AppointmentCategorySerializer

Note, this is exactly the same solution as recommended in the question you linked to.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • That gives me the following error: {"non_field_errors":["Invalid data. Expected a dictionary, but got list."]} – QuestionEverything May 29 '17 at 22:15
  • @HasanIqbalAnik of course it wont work since the serialzier is expecting a post data of a dictionary form and you are trying to pass a list of dictionary which is obviously wont work – Shift 'n Tab May 30 '17 at 04:45