In Django how to bulk_create with a model that has a file field?
I have a model Document:
class Folio():
status = models.OneToOneField(Status, on_delete=models.CASCADE, verbose_name='current status*')
file = models.FileField(
upload_to=create_route_file,
verbose_name='PDF*',
help_text='Format PDF'
)
I need to create objects of it with bulk_create(), to make it with few queries.
I am trying to create objects first with simple Folio() and Status(), putting them into a list and then saving it with bulk_create:
status = Status()
document_parameters = {
'status': status,
...
}
document = Document(**document_parameters)
file = custom_open_file_function(route_to_file)
file_name = 'name...'
file.file.save(file_name, File(file))
That last line raise this error:
ValueError: save() prohibited to prevent data loss due to unsaved related object 'status'.
I don't wanna to do a save() there because that integrity problem with Status and because I don't want to do a query there. All the objetive with bulk_create is to do just one query to statuses and one query for documents, avoiding doing one query per document. But I don't know how to deal with that FileField.