简体   繁体   中英

Django bulk_create while populating ImageField

I am trying bulk_create a list of Person objects from some list of raw parsed API data. the data contains:

  • Id
  • Comment
  • Image

      person = raw_api_data[0] obj = Person( id=person['id'], comment=person['comment'], ) if person['image'] is not None: obj.image.save( '.jpg', ContentFile(person['image'].decode('base-64')), save=False, ) new_persons.append(obj) Person.objects.bulk_create(new_persons) 

I want to be able to avoid creating local image files on file system in case bulk_create fails. ( IntegrityError for example)

Is there any way to avoid creating the file itself before the create process itself while also be able to bulk create all objects with their images?

I did not find any other ideas besides creating the content file before appending the object itself to the bulk list.

To answer your question, no. If you want you are concerned with a huge bulk list try using the concept of batching where you do a bulk insert every 500 items or so. But you will always have to update the list while doing bulk create.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM