Ad

Is There A Faster Way Of Uploading Multiple Files In Django?

- 1 answer

I have a django project where the client can upload multiple files at once. My problem is that for each uploaded file I'm creating a model object - one at a time. Is there a way to do this with bulk create or some other method that is faster.

Views.py:

    images = request.FILES.getlist('images')
    xmls = request.FILES.getlist('xmls')
    jsons = request.FILES.getlist('jsons')
    
    for image in images:
        img_name = (str(image).split('.')[0])
        dp_name = dataset_name+'-'+img_name

        if [xml for xml in xmls if img_name+'.' in str(xml)]:
            xml = [xml for xml in xmls if img_name+'.' in str(xml)][0]
        else:
            xml = None

        if [json for json in jsons if img_name+'.' in str(json)]:
            json = [json for json in jsons if img_name+'.' in str(json)][0]
        else:
            json = None
        dataset.create_datapoint(image, xml, json, username, dp_name)

Models.py:

    def create_datapoint(self, image, xml, json, username, dp_name):
    datapoint = Datapoint.objects.create(
        xml = xml,
        json = json,
        name = dp_name,
        uploaded_by = username,
        img = image,
        belongs_to = self,
    )
    self.num_datapoints += 1
    datapoint.parse_xml()
    self.datapoints.add(datapoint)
    self.save()

    return 
Ad

Answer

@mnislam01 is right but there is a small mistake in the code provided.

Here it is fixed:

data_point_create_list = []
# First create a list of objects.
for image in images:
    dp = Datapoint(xml = xml,
        json = json,
        name = dp_name,
        uploaded_by = username,
        img = image,
        belongs_to = self)
    data_point_create_list.append(dp)


# Then bulk create all the objects.

if data_point_create_list:
    Datapoint.objects.bulk_create(data_point_create_list)

Just needed to assign the newly made datapoint before appending.

Ad
source: stackoverflow.com
Ad