运行脚本时django nonrel ImportError

时间:2014-08-19 20:41:38

标签: python django mongodb importerror django-nonrel

我为我的项目配置了一个虚拟环境,除了当我尝试运行脚本来填充MongoDB数据库时,一切正常。我收到了一个ImportError异常。

我正在使用django mongodb引擎Link和djangotoolbox。我想运行一个脚本来填充我的mongo db,但是当我运行它时会引发以下异常: enter image description here

这是我的项目框架:

enter image description here
这是我的models.py文件:

from django.db import models
from djangotoolbox.fields import EmbeddedModelField, ListField


# Create your models here.
class Category(models.Model):
   name = models.CharField(max_length=128, unique=True)
   pages = ListField(EmbeddedModelField("Page"))

   def __unicode__(self):
       return self.name

class Page(models.Model):
    title = models.CharField(max_length=128)
    url = models.URLField()
    views = models.IntegerField(default=0)

    def __unicode__(self):
        return self.titleenter code here

这是我的settings.py文件:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
from pip.exceptions import BadCommand

BASE_DIR = os.path.dirname(os.path.dirname(__file__))


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'mw7+iswll+f=a!xg9r+*%v=uyv$r_6lqcf)eb27s!4h_!*&9gt'

# 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',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rango',
)

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

ROOT_URLCONF = 'tango_with_django_project.urls'

WSGI_APPLICATION = 'tango_with_django_project.wsgi.application'


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

DATABASES = {
    'default': {
        'ENGINE': 'django_mongodb_engine',
        'NAME': 'tango_with_dj',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/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.6/howto/static-files/

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    os.path.join(BASE_DIR,  'templates'),
)

STATIC_PATH = os.path.join(BASE_DIR,'static')
STATICFILES_DIRS = (
    STATIC_PATH,
)

MEDIA_URL = '/media/'
MEDIA_ROOT 

这是我的填充脚本“populate.py”:

import os

def populate():
    python_cat = add_cat('Python')
    django_cat = add_cat("Django")
    frame_cat = add_cat("Other Frameworks")

    # Print out what we have added to the user.
    for c in Category.objects.all():
        print "Category: "+c.name+" was added!"

def add_cat(name):
    c = Category.objects.get_or_create(name=name)[0]
    return c

# Start execution here!
if __name__ == '__main__':
    print "Starting Rango population script..."
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tango_with_django_project.settings')
    from rango.models import Category
    populate()

我不知道为什么如果在models.py范围内导入djangotoolbox,则无法在填充主范围内访问djangotoolbox。这里有什么问题?

先谢谢。

0 个答案:

没有答案