1

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

Community
  • 1
  • 1
MJP
  • 5,327
  • 6
  • 18
  • 18
  • possible duplicate of [How to post/put json data to ListSerializer](http://stackoverflow.com/questions/27869841/how-to-post-put-json-data-to-listserializer) – Kevin Brown-Silva May 20 '15 at 01:23

1 Answers1

1

As of Django REST framework 3.1, it is not possible to submit multiple values using form data. This is because there is no standardized way of sending lists of data with associations, which is required for handling a ListSerializer.

There are plans to implement HTML JSON forms support, which would end up allowing this to work with a standard. That is milestoned for Django REST framework 3.2, but has not yet been completed.


Until then, it is recommended to use JSON instead of form data. JSON is supported for bulk creation and updating, and you can read about how to impelement it here. The alternative is to use django-rest-framework-bulk which can do the same.

In order to upload a file using JSON, you would need to base64-encode the file and use a custom Base64ImageField to allow them to be uploaded. This is because JSON does not natively support file uploads.

Community
  • 1
  • 1
Kevin Brown-Silva
  • 40,873
  • 40
  • 203
  • 237
  • Ok, thanks for this. The doc didn't make this clear to me - will try and use DRF - bulk! – MJP May 20 '15 at 01:49
  • By the way, is it a good idea to bulk upload several base64 encoded zip files ? One zip can be around 3 - 5 MB. – MJP May 20 '15 at 21:37
  • Nope, probably not. Many web servers will reject requests over a certain size limit (10-25MB). This isn't special to sending files over JSON (though the requests are larger), you would hit similar limitations with a standard file upload. – Kevin Brown-Silva May 20 '15 at 21:39