I'm reading about customizing multiple update here and I haven't figured out in what case the custom ListSerializer
update method is called. I would like to update multiple objects at once, I'm not worried about multiple create or delete at the moment.
From the example in the docs:
# serializers.py
class BookListSerializer(serializers.ListSerializer):
def update(self, instance, validated_data):
# custom update logic
...
class BookSerializer(serializers.Serializer):
...
class Meta:
list_serializer_class = BookListSerializer
And my ViewSet
# api.py
class BookViewSet(ModelViewSet):
queryset = Book.objects.all()
serializer_class = BookSerializer
And my url setup using DefaultRouter
# urls.py
router = routers.DefaultRouter()
router.register(r'Book', BookViewSet)
urlpatterns = patterns('',
url(r'^api/', include(router.urls)),
...
So I have this set up using the DefaultRouter
so that /api/Book/
will use the BookSerializer
.
Is the general idea that if I POST/PUT/PATCH an array of JSON objects to /api/Book/
then the serializer should switch over to BookListSerializer
?
I've tried POST/PUT/PATCH JSON data list to this /api/Book/
that looks like:
[ {id:1,title:thing1}, {id:2, title:thing2} ]
but it seems to still treat the data using BookSerializer
instead of BookListSerializer
. If I submit via POST I get Invalid data. Expected a dictionary, but got list
. and if I submit via PATCH or PUT then I get a Method 'PATCH' not allowed
error.
Question:
Do I have to adjust the allowed_methods
of the DefaultRouter
or the BookViewSet
to allow POST/PATCH/PUT of lists? Are the generic views not set up to work with the ListSerializer
?
I know I could write my own list deserializer for this, but I'm trying to stay up to date with the new features in DRF 3 and it looks like this should work but I'm just missing some convention or some option.