1
我重寫了django-import-export資源的方法。但是隻有父方法被執行。django-import-export override not working
models.py
class Model(models.Model):
modelField1 = models.CharField(
modelField2...
modelField3...
admin.py
class ModelResource(resources.ModelResource):
def before_import(self, dataset, using_transactions, dry_run, **kwargs):
print("INside BEfore IMport")
dataset.headers = ('modelField1', 'modelField2', ...)
del dataset[0]
def get_instance(self, instance_loader, row):
print("Inside get instance")
return False
def get_or_init_instance(self, instance_loader, row):
print("INside Get or init")
instance = self.get_instance(instance_loader, row)
if instance:
return (instance, False)
else:
return (self.init_instance(row), True)
@admin.register(Model)
class ModelAdmin(ImportExportModelAdmin):
class Meta:
model = MOdel
resource_class = ModelResource
list_display = ('modelField1', 'modelField2', ...)
search_fields = ('modelField1', 'modelField2', ...)
我不是在所有獲得打印報表控制檯。錯誤直接從父方法拋出。他們不應該被執行。我對嗎 ?
Line number: 1 - u"Column 'id' not found in dataset. Available columns are: [u'Col1', u'Col2', u'Col2', ...]"
Traceback (most recent call last):
File "/home/aswin/projects/mastercityenv/local/lib/python2.7/site-packages/import_export/resources.py", line 434, in import_row
instance, new = self.get_or_init_instance(instance_loader, row)
File "/home/aswin/projects/mastercityenv/local/lib/python2.7/site-packages/import_export/resources.py", line 258, in get_or_init_instance
instance = self.get_instance(instance_loader, row)
File "/home/aswin/projects/mastercityenv/local/lib/python2.7/site-packages/import_export/resources.py", line 252, in get_instance
return instance_loader.get_instance(row)
File "/home/aswin/projects/mastercityenv/local/lib/python2.7/site-packages/import_export/instance_loaders.py", line 32, in get_instance
params[field.attribute] = field.clean(row)
File "/home/aswin/projects/mastercityenv/local/lib/python2.7/site-packages/import_export/fields.py", line 63, in clean
list(data.keys())))
KeyError: u"Column 'id' not found in dataset. Available columns are: [u'Col1', u'Col2', u'Col2', ...]"
其中Excel文檔中列名的是所有「col1」,「col2的」,...
問:所以,如果你在上面的代碼請注意錯誤,他們是從我父母重載方法。他們爲什麼執行?應該在控制檯上執行打印行嗎?
你在你的'ModelAdmin' – PRMoureu
@PRMoureu對不起調用'ModelResource',而不是'ResourceClass'。那是一個錯字 –