在Windows 8中使用Mongodb设置Django的问题

时间:2014-12-23 10:51:00

标签: django mongodb virtualenv attributeerror

我成功安装了django,目前尝试将它与Mongoengine结合使用。在设置应用程序后使用“python manage.py makemigrate”时,我收到以下错误:

    $PATH$\env\lib\site-packages\mongoengine\django\mongo_auth\models.py:14: RemovedInDjango18Warning: `MongoUserManager.get_query_set` method should be renamed `get_queryset`.
      class MongoUserManager(UserManager):

    Traceback (most recent call last):
      File "manage.py", line 10, in <module>
        execute_from_command_line(sys.argv)
      File "$PATH$\env\lib\site-packages\django\core\management\__init__.py", line 385, in execute_from_command_line
        utility.execute()
      File "$PATH$\env\lib\site-packages\django\core\management\__init__.py", line 354, in execute
        django.setup()
      File "$PATH$\env\lib\site-packages\django\__init__.py", line 21, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "$PATH$\env\lib\site-packages\django\apps\registry.py", line 108, in populate
        app_config.import_models(all_models)
      File "$PATH$\env\lib\site-packages\django\apps\config.py", line 202, in import_models
        self.models_module = import_module(models_module_name)
      File "$PATH$\env\lib\importlib\__init__.py", line 109, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
      File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
      File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
      File "<frozen importlib._bootstrap>", line 1129, in _exec
      File "<frozen importlib._bootstrap>", line 1471, in exec_module
      File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
      File "$PATH$\myproject\polls\models.py", line 4, in <module>
        class Question(models.Model):
      File "$PATH$\myproject\polls\models.py", line 6, in Question
        pub_date = models.DataTimeField('date published')
    AttributeError: 'module' object has no attribute 'DataTimeField'

$ PATH $是我项目的路径。

该应用包含以下models.py:

    from django.db import models
    #from mongoengine import *

    class Question(models.Model):
        question_text = models.CharField(max_length=200)
        pub_date = models.DataTimeField('date published')

    class Choice(models.Model):
        question = models.ForeignKey(Question)
        choice_text = models.CharField(max_length=200)
        votes = models.IntegerField(default=0)

我正在使用django v1.7.1和mongoengine v0.8.0。该错误也可能源自我的项目中的settings.py文件,我编辑该文件以包含mongo db。

"""
Django settings for myproject project.

For more information on this file, see
https://docs.djangoproject.com/en/1.7/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.7/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xg0wnp^w)i@svh13#^v45**4^3v-at#ktre=^n#cw2!6(q__gq'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition


INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'mongoengine.django.mongo_auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls',
)

MIDDLEWARE_CLASSES = (
    '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',
)

ROOT_URLCONF = 'myproject.urls'

WSGI_APPLICATION = 'myproject.wsgi.application'

import mongoengine

# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases

#DATABASES = {
#   'default' : {
#      'ENGINE' : 'django_mongodb_engine',
#      'NAME' : 'my_database'
#   }
#}

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

#DATABASES = {
#    'default': {
#        'ENGINE': 'django.db.backends.dummy',
#    }
#}

AUTHENTICATION_BACKENDS = (
    'mongoengine.django.auth.MongoEngineBackend',
)

MONGOENGINE_USER_DOCUMENT = 'mongoengine.django.auth.User'


#SESSION_ENGINE = 'mongoengine.django.sessions'
#SESSION_SERIALIZER = 'mongoengine.django.sessions.BSONSerializer'

#_MONGODB_USER = 'mongouser'
#_MONGODB_PASSWD = 'password'
#_MONGODB_HOST = 'thehost'
#_MONGODB_NAME = 'thedb'
#_MONGODB_DATABASE_HOST = \
#    'mongodb://%s:%s@%s/%s' \
#    % (_MONGODB_USER, _MONGODB_PASSWD, _MONGODB_HOST, _MONGODB_NAME)

#mongoengine.connect(_MONGODB_NAME, host=_MONGODB_DATABASE_HOST)

#AUTHENTICATION_BACKENDS = (
#    'mongoengine.django.auth.MongoEngineBackend',
#)

# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/

STATIC_URL = '/static/'

此外,我正在使用virtualenv为我的项目创建一个虚拟环境。不确定这是否也会影响该计划。

1 个答案:

答案 0 :(得分:2)

你有一个拼写错误:DataTimeField而不是DateTimeField