我一直试图让 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
(参考文档)导入,并且它有效。这是我所做的:
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)
这真的让我陷入了一个循环。任何帮助将非常感激。