[英]Using mongoengine in Django. Cannot save instance of Document into database
I am trying to create new Document and save it to the database. 我正在尝试创建新文档并将其保存到数据库。 The database in Mongo was created automatically, but no entries are saved.
Mongo中的数据库是自动创建的,但未保存任何条目。 What I am trying to do in my code is the following:
我在代码中尝试做的事情如下:
lesson = Lesson()
lesson.group = Group(group_name='KP-32')
lesson.teacher = Teacher(teacher_name='Vasia')
lesson.room = Room(room_number=32, building_number=323)
lesson.subject = Subject(subject_code=03321, subject_name='Math', hours=33)
lesson.save()
ls = []
for lesson in Lesson.objects:
ls.append(lesson)
But as soon as I try to display ls, I receive in my view (displaying ls) only: id subject teacher group room (names of references???) and nothing is stored into the db. 但是,当我尝试显示ls时,仅在我的视图中显示(显示ls):id主题教师组会议室(引用的名称?),并且没有任何内容存储在数据库中。 Hope to hear some solution for my problem and thanks in advance.
希望听到我的问题的解决方案,并在此先感谢。
Settings 设置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy',
}
}
mongoengine.connect('jack', host='127.0.0.1', port=27017)
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
)
SESSION_ENGINE = 'mongoengine.django.sessions'
Models 楷模
from mongoengine import *
class Room(EmbeddedDocument):
room_number = IntField(default=0)
building_number = IntField(default=0)
capacity = IntField(default=0, required=False)
def __unicode__(self):
return smart_unicode("Room #%d" % self.room_number)
class Teacher(EmbeddedDocument):
teacher_name = StringField(max_length=60)
phone = StringField(max_length=15, required=False)
email = StringField(required=False)
def __unicode__(self):
return smart_unicode(self.teacher_name)
class Subject(EmbeddedDocument):
subject_code = IntField()
subject_name = StringField(max_length=60)
hours = IntField(required=False)
def __unicode__(self):
return smart_unicode(self.subject_name)
class Group(EmbeddedDocument):
group_name = StringField(max_length=20)
specialization = StringField(max_length=60, required=False)
student_count = IntField(required=False)
def __unicode__(self):
return smart_unicode(self.group_name)
class Lesson(Document):
subject = EmbeddedDocumentField(Subject)
teacher = EmbeddedDocumentField(Teacher)
group = EmbeddedDocumentField(Group)
room = EmbeddedDocumentField(Room)
attendance = IntField()
def __unicode__(self):
return smart_unicode("Lesson #%d" % self.id)
Database is created when this line is executed 执行此行时创建数据库
mongoengine.connect('jack', host='127.0.0.1', port=27017)
Also it is working for me. 它也为我工作。
Can you just print the output of this for me: 您能为我打印输出吗?
ls[0].__dict__
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.