Im using django import-export module and Im following this documentation:
https://simpleisbetterthancomplex.com/packages/2016/08/11/django-import-export.html#importing-data
I want to create an upload-page where the user can upload multiple files.
This is how far I got:
views.py
def upload_data(request):
if request.method == 'POST':
wo_resource = WorkordersResource()
pl_resource = PlantResource()
se_resource = SeriesResource()
re_resource = ResourcesResource()
rd_resource = ResourceDemandsResource()
dataset = Dataset()
wo_data = request.FILES.get('workorders_key', None)
pl_data = request.FILES.get('plants_key', None)
se_data = request.FILES.get('series_key', None)
re_data = request.FILES.get('resources_key', None)
rd_data = request.FILES.get('resourcedemands_key', None)
wo_imported_data = dataset.load(wo_data.read())
pl_imported_data = dataset.load(pl_data.read())
se_imported_data = dataset.load(se_data.read())
re_imported_data = dataset.load(re_data.read())
rd_imported_data = dataset.load(rd_data.read())
wo_result = wo_resource.import_data(dataset, dry_run=True)
pl_result = pl_resource.import_data(dataset, dry_run=True)
se_result = se_resource.import_data(dataset, dry_run=True)
re_result = re_resource.import_data(dataset, dry_run=True)
rd_result = rd_resource.import_data(dataset, dry_run=True)
if not wo_result.has_errors():
wo_resource.import_data(dataset, dry_run=False)
elif not pl_result.has_errors():
pl_resource.import_data(dataset, dry_run=False)
elif not se_result.has_errors():
se_resource.import_data(dataset, dry_run=False)
elif not re_result.has_errors():
re_resource.import_data(dataset, dry_run=False)
elif not rd_result.has_errors():
rd_resource.import_data(dataset, dry_run=False)
else:
print(result)
return render(request,'import.html')
import.html
<form class="importform" method="post" enctype="multipart/form-data"> {% csrf_token %}
<div class="formlabel">
<label class="uploadlabel">Workorders</label>
<input class="uploadform" type="file" name="workorders_key" placeholder="Workorders">
</div>
<div class="formlabel">
<label class="uploadlabel">Plants</label>
<input class="uploadform" type="file" name="plants_key" placeholder="Plants">
</div>
<div class="formlabel">
<label class="uploadlabel">Series</label>
<input class="uploadform" type="file" name="series_key" placeholder="Series">
</div>
<div class="formlabel">
<label class="uploadlabel">Resources</label>
<input class="uploadform" type="file" name="resources_key" placeholder="Resources">
</div>
<div class="formlabel">
<label class="uploadlabel">Resource Demands</label>
<input class="uploadform" type="file" name="resourcedemands_key" placeholder="Resource Demands">
</div>
<button type="submit" class="btn btn-primary">Upload</button>
</form>
The data from the first file gets imported, the data from the other files not.
Thank you
request.FILES.get('myfile')
). So depending on which one, you just instantiate the correspondingModelResource
. – Convolverequest.FILES['KEY']
to the corresponding key of the form, right? – Juxtaposeif ... else...
clause – Convolveget()
instead, it returnsNone
if the key doesn’t exist.request.FILES.get('plants_key')
. This is basic python. Or if you don’t wantNone
but an empty file as defaultrequest.FILES.get('plants_key', File())
. – Convolve