在 Django 中使用 mongoengine。无法将文档实例保存到数据库中
Using mongoengine in Django. Cannot save instance of Document into database
我正在尝试创建新文档并将其保存到数据库中。 Mongo 中的数据库是自动创建的,但没有保存任何条目。我想在我的代码中做的事情如下:
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)
但是当我尝试显示 ls 时,我的视图(显示 ls)中只收到:
id 主题教师组房间(参考名称???)并且没有任何内容存储到数据库中。
希望听到一些解决我的问题的方法,在此先感谢。
设置
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'
型号
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)
执行此行时创建数据库
mongoengine.connect('jack', host='127.0.0.1', port=27017)
它也对我有用。
你能帮我打印一下这个输出吗:
ls[0].__dict__
我正在尝试创建新文档并将其保存到数据库中。 Mongo 中的数据库是自动创建的,但没有保存任何条目。我想在我的代码中做的事情如下:
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)
但是当我尝试显示 ls 时,我的视图(显示 ls)中只收到: id 主题教师组房间(参考名称???)并且没有任何内容存储到数据库中。 希望听到一些解决我的问题的方法,在此先感谢。
设置
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'
型号
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)
执行此行时创建数据库
mongoengine.connect('jack', host='127.0.0.1', port=27017)
它也对我有用。
你能帮我打印一下这个输出吗:
ls[0].__dict__