我使用的是django+mongoengine。
我正在按照官方教程学习,但我不知道如何在我的数据库中引入数据。
我不知道我是否必须使用 django 控制台、python 控制台或 mongodb
这是教程的链接:MongoengineTutorial
使用mongo我发现如下错误
(reb_env)root@marcproves:/REBORN/REBORN_PROJ# mongo
MongoDB shell version: 2.4.9
connecting to: test
Server has startup warnings:
Tue Apr 1 05:37:35.300 [initandlisten]
Tue Apr 1 05:37:35.300 [initandlisten] ** WARNING: You are running in OpenVZ. This is known to be broken!!!
Tue Apr 1 05:37:35.301 [initandlisten]
> ross = User(email='ross@example.com', first_name='Ross', last_name='Lawley').save()
Tue Apr 1 06:22:38.046 ReferenceError: User is not defined
> show dbs
local 0.03125GB
reborn 0.203125GB
> use reborn
switched to db reborn
> ross = User(email='ross@example.com', first_name='Ross', last_name='Lawley').save()
Tue Apr 1 06:22:51.791 ReferenceError: User is not defined
我刚刚创建了一个应用程序并添加了教程中的以下代码:
GNU nano 2.2.6 File: models.py
//from django.db import models
from mongoengine import *
# Create your models here.
class User(Document):
email = StringField(required=True)
first_name = StringField(max_length=50)
last_name = StringField(max_length=50)
class Post(Document):
title = StringField(max_length=120, required=True)
author = ReferenceField(User, reverse_delete_rule=CASCADE)
tags = ListField(StringField(max_length=30))
comments = ListField(EmbeddedDocumentField(Comment))
meta = {'allow_inheritance': True}
class TextPost(Post):
content = StringField()
class ImagePost(Post):
image_path = StringField()
class LinkPost(Post):
link_url = StringField()
class Comment(EmbeddedDocument):
content = StringField()
name = StringField(max_length=120)
更新:在 django shell 中运行
(reb_env)root@marcproves:/REBORN/REBORN_PROJ# python manage.py shell
Python 2.7.3 (default, Sep 26 2013, 20:03:06)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from mongoengine import *
>>> connect('reborn')
MongoClient('localhost', 27017)
>>> ross = User(email='ross@example.com', first_name='Ross', last_name='Lawley').save()
Traceback (most recent call last):
File "<console>", line 1, in <module>
NameError: name 'User' is not defined
>>>
同样的错误,User is not defined,我该怎么办?
最佳答案
好吧,我终于解决了这个问题。
在模型中,我必须一如既往地声明数据库是怎样的......
(模型.py)
# Create your models here.
from mongoengine import *
class User(Document):
email = StringField(required=True)
first_name = StringField(max_length=50)
last_name = StringField(max_length=50)
class Comment(EmbeddedDocument):
content = StringField()
name = StringField(max_length=120)
class Post(Document):
title = StringField(max_length=120, required=True)
author = ReferenceField(User)
tags = ListField(StringField(max_length=30))
comments = ListField(EmbeddedDocumentField(Comment))
meta = {'allow_inheritance': True}
class TextPost(Post):
content = StringField()
class ImagePost(Post):
image_path = StringField()
class LinkPost(Post):
link_url = StringField()
在 views.py(或 python 控制台)...
from django.shortcuts import render
# Create your views here.
from django.http import HttpResponse
from game.models import * //Import the models
from mongoengine import * //Import things like connect
def testview(request):
connect('reborn')//Connect to DB
//Let's add some data!
article = User(email = 'test@title.com',first_name = 'test content')
article.save()
return HttpResponse("SAVED")
关于django - 在 MongoEngine + Django 中添加数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22783135/