I am using Django REST framework for API and Angular SPA with Restangular for comunication with the API. Sometimes I have to add more than one objects for adding and I think that I can send them together in array and make one request.
When I try to do this from the Restangular I recive error for Wrong input. If I try to add more then one object from the REST framework web interface I am passing objects or array of objects:
// this { "text": "gdhg", },{ "text": "gdhg", },{ "text": "gdhg", }
// or this [{ "text": "gdhg", },{ "text": "gdhg", },{ "text": "gdhg", }]
But I receive ParseError. Where I am wrong, what I have to change or how to do it properly.
Another example that supports posting an array as well as posting a single object. Might be useful for anyone else looking for such an example.
Building on vibhor's answer:
Make your view inherit from this mixin class to automatically determine if a
many=True
serializer should be used.I am not sure if the problem still exist. But the solution suggested by fiver did not work for me. What works for me is overriding the
get_serializer
method ONLY.If you will notice I am setting default
many=True
in arguments ofget_serializer
. Apart from that nothing is required. Overridng of create method is also not required.Also if you are defining the pre_save and post_save method in the views, expects the list(iterable) as the argument(as you are posting the list) of method not just a single object.
Here's an example for setting up bulk POSTing in a ListCreateAPIView using the Django REST Framework:
The important part here is the
many=True
argument to theget_serializer()
method. Then, to make Angular play nice with this, you can define a service factory as:Where the important part is the
isArray: true
. If you want to preserve posting single JSON objects, you could changesave
above to something likesaveBulk
or similar.