1) I would like to create multiple model instances with one API call, like asked here: How do I create multiple model instances with Django Rest Framework? I tried the solution named in the link, with no success.
I am trying to upload multiple files in one API call. Result: the files are uploaded (only once I overwrote perform_create) BUT only one instance is created (if I send two files, only the latter is created as an instance).
My code:
class FileUploadSerializer(serializers.ModelSerializer):
def __init__(self, *args, **kwargs):
many = kwargs.pop('many', True)
super(FileUploadSerializer, self).__init__(many=many, *args, **kwargs)
class Meta:
model = FileUpload
read_only_fields = ('created', 'datafile', 'owner')
class FileUploadViewSet(viewsets.ModelViewSet):
queryset = FileUpload.objects.all()
serializer_class = FileUploadSerializer
parser_classes = (MultiPartParser, FormParser, )
def perform_create(self, serializer):
file_list = self.request.data.getlist('file')
for item in file_list:
serializer.save(file=item)
Am I on the right track? The documentation http://www.django-rest-framework.org/api-guide/serializers/#dealing-with-multiple-objects mentions: "To serialize a queryset or list of objects instead of a single object instance, you should pass the many=True flag when instantiating the serializer. You can then pass a queryset or list of objects to be serialized." => It should be possible ...
2) Is it better to use django-rest-framework-bulk for this? https://github.com/miki725/django-rest-framework-bulk