gpt4 book ai didi

python - django-import-export 覆盖不起作用

转载 作者:行者123 更新时间:2023-11-30 22:28:14 26 4
gpt4 key购买 nike

我正在重写 django-import-export 资源的方法。但只执行父方法。

模型.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”...

问题:所以如果您注意到上面代码中的错误,它们来 self 重载的父方法。他们为何被处决?应该执行.tmy 代码在控制台中打印打印行吗?

最佳答案

根据 documentation ,不需要嵌套 Meta 类。所以 Admin 类应该是这样的:

@admin.register(Model)
class ModelAdmin(ImportExportModelAdmin):
resource_class = ModelResource
list_display = ('modelField1', 'modelField2', ...)
search_fields = ('modelField1', 'modelField2', ...)

关于python - django-import-export 覆盖不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46671824/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com