简体   繁体   中英

django-import-export override not working

I am overriding the django-import-export resource's methods. But only parent methods are executed.

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', ...)

I am not at all getting print statements in console. Error is thrown directly from the parent methods. They should not be executed at all. AM I right ?

 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', ...]"

where column names in Excel document were "Col1", "Col2", ...

Question: So if you note the errors in the above code, they were from parent methods which I overloaded. Why are they executed? Shouldn.tmy code be executed printing the print lines in console ?

As per documentation , Nested Meta class is not required. So the Admin class should be like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM