如何在不更改原始模型的情况下向嵌入式文档添加其他字段
How to add additional fields to embedded documents without changing the original model
我想在将文档嵌入其他文档之前向文档添加额外的属性,但我不知道该怎么做。
这是我的代码以及到目前为止我尝试过的代码:
from mongoengine import *
from datetime import datetime
class User(Document):
name = StringField(max_length=80, required=True)
created_at = DateTimeField(default=datetime.now(), required=True)
updated_at = DateTimeField(default=datetime.now(), required=True)
meta = {
'collection': 'users'
}
def save(self, *args, **kwargs):
self.updated_at = datetime.now()
return super(User, self).save(*args, **kwargs)
class Stream(Document):
users = EmbeddedDocumentListField(document_type='User')
created_at = DateTimeField(default=datetime.now(), required=True)
updated_at = DateTimeField(default=datetime.now(), required=True)
meta = {
'collection': 'streams'
}
def save(self, *args, **kwargs):
self.updated_at = datetime.now()
return super(Stream, self).save(*args, **kwargs)
当我将 user document
嵌入到流的用户 EmbeddedDocumentListField
时,它将被添加并看起来像这样
{
"_id" : ObjectId("57e6123fe8c39b18b1a13431"),
"users" : [
{
"_id" : ObjectId("57e6123fe8c39b18b1a13432"),
"name": "Rohit Khatri",
"created_at" : ISODate("2016-09-24T11:12:23.182Z"),
"updated_at" : ISODate("2016-09-24T11:12:23.301Z")
}
],
"created_at" : ISODate("2016-09-24T11:12:23.189Z"),
"updated_at" : ISODate("2016-09-24T11:12:23.323Z")
}
现在我想在用户文档中嵌入其他属性,例如角色。这是我尝试过的:-
user = User.objects.create(name='Rohit Khatri')
user.roles = ['admin','writer']
stream = Stream.objects.create()
stream.users.append(user)
stream.save()
但它没有添加角色字段,如果有人能帮助我,将不胜感激。
谢谢
使用更新方法设置新属性
user.update(set__roles = ['admin','writer'])
我想在将文档嵌入其他文档之前向文档添加额外的属性,但我不知道该怎么做。
这是我的代码以及到目前为止我尝试过的代码:
from mongoengine import *
from datetime import datetime
class User(Document):
name = StringField(max_length=80, required=True)
created_at = DateTimeField(default=datetime.now(), required=True)
updated_at = DateTimeField(default=datetime.now(), required=True)
meta = {
'collection': 'users'
}
def save(self, *args, **kwargs):
self.updated_at = datetime.now()
return super(User, self).save(*args, **kwargs)
class Stream(Document):
users = EmbeddedDocumentListField(document_type='User')
created_at = DateTimeField(default=datetime.now(), required=True)
updated_at = DateTimeField(default=datetime.now(), required=True)
meta = {
'collection': 'streams'
}
def save(self, *args, **kwargs):
self.updated_at = datetime.now()
return super(Stream, self).save(*args, **kwargs)
当我将 user document
嵌入到流的用户 EmbeddedDocumentListField
时,它将被添加并看起来像这样
{
"_id" : ObjectId("57e6123fe8c39b18b1a13431"),
"users" : [
{
"_id" : ObjectId("57e6123fe8c39b18b1a13432"),
"name": "Rohit Khatri",
"created_at" : ISODate("2016-09-24T11:12:23.182Z"),
"updated_at" : ISODate("2016-09-24T11:12:23.301Z")
}
],
"created_at" : ISODate("2016-09-24T11:12:23.189Z"),
"updated_at" : ISODate("2016-09-24T11:12:23.323Z")
}
现在我想在用户文档中嵌入其他属性,例如角色。这是我尝试过的:-
user = User.objects.create(name='Rohit Khatri')
user.roles = ['admin','writer']
stream = Stream.objects.create()
stream.users.append(user)
stream.save()
但它没有添加角色字段,如果有人能帮助我,将不胜感激。
谢谢
使用更新方法设置新属性
user.update(set__roles = ['admin','writer'])