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. 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. 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__
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.