我正在尝试在provide how this field is named in datasource
中扩展bmihelac/django-import-export
的管理员定义,并收到错误Attribute Error 'module' object has no attribute "Field"
:
在models.py
中class Author(models.Model):
author = models.CharField( max_length=50)
title = models.CharField( max_length=150)
在admin.py中
#admin.py
from blog.models import *
from django.contrib import admin
from import_export.admin import ImportExportModelAdmin
from import_export import resources
from import_export import fields
class AuthorResource(resources.ModelResource):
author= fields.Field(column_name='myname')
class Meta:
model = Author
class AuthorAdmin(ImportExportModelAdmin,admin.ModelAdmin):
search_fields = ('author', 'title',)
list_display = ('author', 'title',)
resource_class = AuthorResource
感谢您的关注。
整个追溯
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/blog/author/
Django Version: 1.6.2
Python Version: 2.7.6
Installed Applications:
('blog',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'suit',
'django.contrib.admin',
'south',
'django_select2',
'suit_redactor',
'suit_ckeditor',
'import_export',
'reversion',
'django_extensions')
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 "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
101. resolver_match = resolver.resolve(request.path_info)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in resolve
318. for pattern in self.url_patterns:
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in url_patterns
346. patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Python27\lib\site-packages\django\core\urlresolvers.py" in urlconf_module
341. self._urlconf_module = import_module(self.urlconf_name)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "D:\Documents\GitHub\GoodDjango\GoodDjango\urls.py" in <module>
7. admin.autodiscover()
File "C:\Python27\lib\site-packages\django\contrib\admin\__init__.py" in autodiscover
29. import_module('%s.admin' % app)
File "C:\Python27\lib\site-packages\django\utils\importlib.py" in import_module
40. __import__(name)
File "D:\Documents\GitHub\GoodDjango\blog\admin.py" in <module>
31. class AuthorResource(resources.ModelResource):
File "D:\Documents\GitHub\GoodDjango\blog\admin.py" in AuthorResource
32. author= fields.Field(column_name='myname')
Exception Type: AttributeError at /admin/blog/author/
Exception Value: 'module' object has no attribute 'Field'
因为我导入了import_export相关的类和motheds,我找不到原因。
答案 0 :(得分:0)
请检查__init__.py
C:\Python27\lib\site-packages\import_export\
如果您使用的是virtualenv
,则表明它尚未激活。
此处你有错误:
class AuthorAdmin(ImportExportModelAdmin):
search_fields = ('author', 'title',)
list_display = ('author', 'title',)
resource_class = AuthorResource
因为ImportExportModelAdmin已经有ModelAdmin:
class ImportExportModelAdmin(ImportExportMixin, admin.ModelAdmin):
"""
Subclass of ModelAdmin with import/export functionality.
"""