问题
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
回答1:
There has been a dependency update that broke django-import-export. You just need to force tablib version to 0.14.0.
There's an issue on django-import-export github: #1064
回答2:
You need to check that your file uploads are not Null. request.FILES will return the key error if its contents are null.
You need to set a default value with request.FILES.get('file', <default>)
where <default>
is either another file or None
.
For example in your code, if we take None as the default value, the lines:
wo_data = request.FILES['workorders_key']
pl_data = request.FILES['plants_key']
se_data = request.FILES['series_key']
re_data = request.FILES['resources_key']
rd_data = request.FILES['resourcedemands_key']
should become:
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)
来源:https://stackoverflow.com/questions/59752539/django-import-export-upload-multiple-files