Django import-export导入按钮抛出了神秘的TypeError

时间:2015-07-17 20:57:26

标签: django django-import-export

我一直试图让django-import-export与我网站的管理页面一起工作,但是遇到了一堵砖墙。目前我正在使用一个空数据库(已从模型创建模式,但未添加任何数据)。 “导出”按钮似乎工作正常,但单击导入按钮会抛出一个TypeError,我无法解释它的生命。这是:

    TypeError at /admin/new_bridge/wordtable/import/

__init__() takes exactly 3 arguments (1 given)

Request Method:     GET
Request URL:    http://localhost:8000/admin/new_bridge/wordtable/import/
Django Version:     1.8.3
Exception Type:     TypeError
Exception Value:    

__init__() takes exactly 3 arguments (1 given)

Exception Location:     /usr/local/lib/python2.7/dist-packages/import_export/admin.py in import_action, line 178
Python Executable:  /usr/bin/python
Python Version:     2.7.6
Python Path:    

['/home/jack/ds/bridge-repo',
 '/opt/ros/indigo/lib/python2.7/dist-packages',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/pymodules/python2.7',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client',
 '/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode',
 '/opt/new_bridge/']

Server time:    Fri, 17 Jul 2015 15:38:33 -0500

这是该错误的追溯:

    Environment:


Request Method: GET
Request URL: http://localhost:8000/admin/new_bridge/wordtable/import/

Django Version: 1.8.3
Python Version: 2.7.6
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'new_bridge',
 'import_export')
Installed Middleware:
('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:
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/utils/decorators.py" in _wrapped_view
  110.                     response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/views/decorators/cache.py" in _wrapped_view_func
  57.         response = view_func(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/admin/sites.py" in inner
  233.             return view(request, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/import_export/admin.py" in import_action
  178.         resource = self.get_import_resource_class()()

Exception Type: TypeError at /admin/new_bridge/wordtable/import/
Exception Value: __init__() takes exactly 3 arguments (1 given)

如果它有帮助,我尝试通过manage.py shell导入(引用the docs),并且它有效。这是我做的:

import tablib
from import_export import resources
from new_bridge.models import *
book_resource = resources.modelresource_factory(model=WordTable)()
dataset = tablib.Dataset(['1', 'title'], headers=['id','title'])
result = book_resource.import_data(dataset, dry_run=False)
print result.has_errors()
False

这是有问题的模型(来自models.py):

class WordTable(models.Model):
    id = models.IntegerField(primary_key=True)
    title = models.CharField(db_column='TITLE', max_length=17, blank=True)
    def __unicode__(self):
        return self.title
    class Meta:
        managed = True
        db_table = 'word_table'

...以及manage.py的相关部分:

from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from import_export import resources
from new_bridge.models import WordTable

class WordTableResource(ImportExportModelAdmin):
    class Meta:
        model = WordTable
        fields = ('title')

class WordTableAdmin(ImportExportModelAdmin):
    fields = ['title']
    resource_class = WordTableResource
    pass

# Register models w admin to make them editable:
admin.site.register(WordTable, WordTableAdmin)

这真的让我经历了一个循环。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

您的WordTableResource课程继承自ImportExportModelAdmin而不是resources.ModelResource