django import-export:意外的关键字参数

时间:2015-09-28 05:59:28

标签: python django

我正在尝试制作django的导出/导入工作(django-import-export.readthedocs.org)

问题是它希望每个文件头中都有“id”来识别它。我添加了before_import函数,但它给了我以下错误:

TypeError("before_import() got an unexpected keyword argument 'file_name'",)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/import_export/resources.py", line 340, in import_data
self.before_import(dataset, real_dry_run, **kwargs)
TypeError: before_import() got an unexpected keyword argument 'file_name'

以下是来自管理员的代码

from django.contrib import admin
from django.db import models
# Register your models here.
from import_export import resources
from import_export import fields
from keywords.models import Book
from import_export.admin import ImportExportModelAdmin


class BookResource(resources.ModelResource):

    def before_import(self, dataset, dry_run):

        if dataset.headers:
            dataset.headers = [str(header).lower().strip() for header in dataset.headers]
        if 'id' not in dataset.headers:
            dataset.headers.append('id')    

    class Meta:
        model = Book
        fields = ('name', 'price',)



class BookAdmin(ImportExportModelAdmin):
    resource_class = BookResource
    pass




admin.site.register(Book, BookAdmin)

1 个答案:

答案 0 :(得分:1)

快速查看文档会显示您的方法签名错误...

来自the docs

before_import(dataset, dry_run, **kwargs)

您的代码应该是

def before_import(self, dataset, dry_run, **kwargs):