> Le 21 avr. 2017 à 04:17, Robin Lery <[email protected]> a écrit : > > I have a very simple model and its related serializer and views: > > class Page(models.Model): > user = models.ForeignKey(User) > title = models.CharField(max_length=255) > pub_date = models.DateTimeField(default=timezone.now) > > class PageSerializer(serializers.ModelSerializer): > class Meta: > model = Page > fields = ('user', 'title', 'pub_date') > > class PageViewSet(viewsets.ModelViewSet): > queryset = Page.objects.all() > serializer_class = PageSerializer > Now I can post like this: > > { > "user": 1, > "title": "Lorem ipsum" > } > This works fine. But I would like to post multiple objects like this: > > [ > { > "user": 1, > "title": "Lorem ipsum one" > }, > { > "user": 1, > "title": "Lorem ipsum two" > } > ] > But this gives me an error: > > "non_field_errors": [ > > "Invalid data. Expected a dictionary, but got list." > ] > So to accept multple objects I modified the view as per the doc: > > class PageViewSet(viewsets.ModelViewSet): > queryset = Page.objects.all() > serializer_class = PageSerializer(queryset, many=True) > > But I am getting an error: > > TypeError at /api/blog/pages/ > > 'ListSerializer' object is not callable > What am I missing here?
it’s serializer_class, not serializer_instance. You can’t instantiate the serializer there. In order to deal with multiples items, you better override the `list` method to handle multiple objects creation and have an explicit serializer instantiation. Regards, Xavier Ordoquy, Linovia. -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at https://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/645FD5F7-649C-4454-9450-93AD87B6FA5C%40linovia.com. For more options, visit https://groups.google.com/d/optout.

