I would like to save and update multiple instances using the Django Rest Framework with one API call. For example, let's say I have a "Classroom" model that can have multiple "Teachers". If I wanted to create multiple teachers and later update all of their classroom numbers how would I do that? Do I have to make an API call for each teacher?
I know currently we can't save nested models, but I would like to know if we can save it at the teacher level. Thanks!
Here's another solution, you don't need to override your serializers
__init__
method. Just override your view's (ModelViewSet)'create'
method. Noticemany=isinstance(request.data,list)
. Heremany=True
when you send an array of objects to create, andFalse
when you send just the one. This way, you can save both an item and a list!I came up with simple example in
post
Serializers.py
Views.py
These line are the actual logic of Multiple Instance -
If you are confused with many=True, see this
When we send data it will be inside
list
somewhat like this -I came to a similar conclusion as Daniel Albarral, but here's a more succinct solution: