尝试将 XLSX 文件中的两张工作表中的数据导入两个不同的模型 - 无法引导数据。我正在使用 pyexcel 包来导入 xlsx 数据。我正在关注http://django.pyexcel.org/en/latest/
注意:我试图从单张单模型加载数据。这已成功加载,没有任何问题。
但是,将两个工作表数据导入两个不同的模型时出现问题(具有外键关系)
使用的型号:
class Supplier(models.Model):
Supplier_Number = models.IntegerField(unique=True)
Supplier_Name = models.CharField(max_length=150)
Supplier_Contracting_Entity = models.CharField(max_length=150)
Applicable_Brands = models.CharField(max_length=150)
Currency_To_Be_Paid = models.CharField(max_length=3)
Other_Remarks = models.CharField(max_length=150)
Term = models.CharField(max_length=15)
Notes = models.CharField(max_length=150)
Fee_Calculation_Basis = models.CharField(max_length=25)
def __str__(self):
return self.Supplier_Name
class Tax_Vat(models.Model):
Supplier = models.ForeignKey(Supplier, on_delete=models.CASCADE)
#Supplier_Number = models.IntegerField()
#-- Above will store the Supplier ID, which is generated automatically
COUNTRY =models.CharField(max_length=25)
DEDUCTION_TYPE =models.CharField(max_length=5)
NGR_FLAG =models.CharField(max_length=5)
TAX_VAT_RATE = models.FloatField()
def __str__(self):
return self.COUNTRY
视图.py:
class UploadFileForm(forms.Form):
file = forms.FileField()
def sup_import(request):
if request.method == "POST":
form = UploadFileForm(request.POST,request.FILES)
def tax_func(row):
q = Supplier.objects.filter(Supplier_Number=row[0])[0]
row[0] = q
return row
#if form.is_valid():
request.FILES['myfile'].save_book_to_database(
models=[Supplier, Tax_Vat],
initializers=[None, tax_func],
mapdicts=[
{ "Supplier_Number": "Supplier_Number",
"Supplier_Name": "Supplier_Name",
"Supplier_Contracting_Entity":"Supplier_Contracting_Entity",
"Applicable_Brands":"Applicable_Brands",
"Currency_To_Be_Paid":"Currency_To_Be_Paid",
"Other_Remarks":"Other_Remarks",
"Term":"Term",
"Notes":"Notes",
"Fee_Calculation_Basis":"Fee_Calculation_Basis"
},
{
"Supplier_Number":"Supplier",
"COUNTRY":"COUNTRY",
"DEDUCTION_TYPE":"DEDUCTION_TYPE",
"NGR_FLAG":"NGR_FLAG",
"TAX_VAT_RATE":"TAX_VAT_RATE"
}
]
)
return redirect('Royalty_upload.html')
# else:
#return HttpResponseBadRequest()
else:
form = UploadFileForm()
return render(
request,
'Royalty_upload.html',
{
'form': form,
'title': 'Import excel data into database example',
'header': 'Please upload Royalty_Input_Import_Data_All.xlsx:'
})
错误详情:
Exception at /royalty_supplier
Sheet: Supplier does not match any given models.Please be aware of case sensitivity.
Request Method: | POST
Request URL: | http://127.0.0.1:8000/royalty_supplier
Django Version: | 3.0.3
Exception Type: | Exception
Exception Value: | Sheet: Supplier does not match any given models.Please be aware of case sensitivity
Exception Location: | C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel_io\database\importers\django.py in create_sheet, line 85
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/royalty_supplier
Django Version: 3.0.3
Python Version: 3.7.1
Installed Applications:
['django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'import_export',
'RECON.apps.ReconConfig']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware']
Traceback (most recent call last):
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django\core\handlers\base.py", line 113, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\manoj.remala\PycharmProjects\MANOJ_IVY\IREC_WEB\RECON\views.py", line 121, in sup_import
"TAX_VAT_RATE":"TAX_VAT_RATE"
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\django_excel_init.py", line 63, in save_book_to_database
pe.save_book_as(**params)
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel\core.py", line 112, in save_book_as
return sources.save_book(book, **dest_keywords)
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel\internal\core.py", line 56, in save_book
return _save_any(a_source, book)
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel\internal\core.py", line 60, in _save_any
a_source.write_data(instance)
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel\plugins\sources\db_sources.py", line 111, in write_data
**self._keywords
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel\plugins\renderers\django.py", line 75, in render_book_to_stream
**keywords
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel_io\io.py", line 131, in save_data
**keywords
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel_io\io.py", line 148, in store_data
writer.write(data)
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel_io\book.py", line 218, in write
sheet_writer = self.create_sheet(sheet_name)
File "C:\Users\manoj.remala\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pyexcel_io\database\importers\django.py", line 85, in create_sheet
+ "Please be aware of case sensitivity."
Exception Type: Exception at /royalty_supplier
Exception Value: Sheet: Supplier does not match any given models.Please be aware of case sensitivity.