django - Creating multiple model instances in DRF3 -


1) create multiple model instances 1 api call, asked here: how create multiple model instances django rest framework? tried solution named in link, no success.

i trying upload multiple files in 1 api call. result: files uploaded (only once overwrote perform_create) 1 instance created (if send 2 files, latter created 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')         item in file_list:             serializer.save(file=item) 

am on right track? documentation http://www.django-rest-framework.org/api-guide/serializers/#dealing-with-multiple-objects mentions: "to serialize queryset or list of objects instead of single object instance, should pass many=true flag when instantiating serializer. can pass queryset or list of objects serialized." => should possible ...

2) better use django-rest-framework-bulk this? https://github.com/miki725/django-rest-framework-bulk

as of django rest framework 3.1, not possible submit multiple values using form data. because there no standardized way of sending lists of data with associations, required handling listserializer.

there plans implement html json forms support, end allowing work standard. milestoned django rest framework 3.2, has not yet been completed.


until then, recommended use json instead of form data. json supported bulk creation , updating, and can read how impelement here. alternative use django-rest-framework-bulk can same.

in order upload file using json, need base64-encode file , use a custom base64imagefield allow them uploaded. because json not natively support file uploads.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -