Django Rest Framework - '模块'对象没有属性' HStoreField'

时间:2015-03-20 10:34:16

标签: python django django-rest-framework

我最近更新了一个与Django rest framework 2.4.2一起使用的项目到新的3.1版本,但现在我得到了一个:

'module' object has no attribute 'HStoreField'

包括"/"

在内的所有端点都会发生这种情况

有什么建议吗? 我的Django版本是1.8和python 2.7.6

回溯:

Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/

Django Version: 1.8.dev20140910173004
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',
'rest',
'rest_framework',
'geoposition',
'rest_framework_swagger',
'PIL',
'rest_framework.authtoken',
'corsheaders')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware')

Traceback:
File "/Users/marcus/PycharmProjects/django-framework/django-trunk/django/core/handlers/base.py" in get_response
  112.                 resolver_match = resolver.resolve(request.path_info)
File "/Users/marcus/PycharmProjects/django-framework/django-trunk/django/core/urlresolvers.py" in resolve
  362.             for pattern in self.url_patterns:
File "/Users/marcus/PycharmProjects/django-framework/django-trunk/django/core/urlresolvers.py" in url_patterns
  398.         patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/marcus/PycharmProjects/django-framework/django-trunk/django/core/urlresolvers.py" in urlconf_module
  392.             self._urlconf_module = import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)
File "/Users/marcus/Google Drive/Spring2015/AppContest/MyApplication/Backend/Backend/urls.py" in <module>
  3. from rest_framework import routers
File "/Library/Python/2.7/site-packages/rest_framework/routers.py" in <module>
  23. from rest_framework import views
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in <module>
  91. class APIView(View):
File "/Library/Python/2.7/site-packages/rest_framework/views.py" in APIView
  94.     renderer_classes = api_settings.DEFAULT_RENDERER_CLASSES
File "/Library/Python/2.7/site-packages/rest_framework/settings.py" in __getattr__
  206.             val = perform_import(val, attr)
File "/Library/Python/2.7/site-packages/rest_framework/settings.py" in perform_import
  158.         return [import_from_string(item, setting_name) for item in val]
File "/Library/Python/2.7/site-packages/rest_framework/settings.py" in import_from_string
  170.         module = importlib.import_module(module_path)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py" in import_module
  37.     __import__(name)
File "/Library/Python/2.7/site-packages/rest_framework/renderers.py" in <module>
  20. from rest_framework import exceptions, serializers, status, VERSION
File "/Library/Python/2.7/site-packages/rest_framework/serializers.py" in <module>
  1354.     ModelSerializer.serializer_field_mapping[postgres_fields.HStoreField] = CharMappingField

Exception Type: AttributeError at /
Exception Value: 'module' object has no attribute 'HStoreField'

1 个答案:

答案 0 :(得分:2)

安装Django 1.8的最新候选版应该可以解决这个问题。

您正在运行1.8.dev20140910173004,它已经支持某些Postgres字段,但尚未支持HStoreField。 Django REST框架仅检查django.contrib.postgres,然后假定HStoreField也可用。对于你的Django版本,情况并非如此。

如果pip install -u没有为您提供最新版本,请尝试删除并重新安装Django。