I am trying to make simple rating application, where I am using MongoDB via MongoEngine ODM. I have some troubles with understanding the ReferencingField()
. I am tracking changes of User()
documents with fields date_created
and date_modified
in User
collection. Both should include the datetime and reference to user.
I solved the referencing via adding ObjectID
to date_created
and date_modified
after first saving the User
. Is it in the end same thing as using ReferenceField()
? Is there more simpler way to archieve this? For example use ReferenceField()
in the ModificationInformation()
class?
My implementation is as follows:
class ModificationInformation(EmbeddedDocument):
user = ObjectIdField()
date = DateTimeField(default=datetime.datetime.utcnow)
class User(Document):
username = StringField()
first_name = StringField()
last_name = StringField()
date_created = EmbeddedDocumentField(ModificationInformation)
date_modified = EmbeddedDocumentListField(ModificationInformation)
# Create user
user_1 = User(
username = "jodo",
first_name = "John",
last_name = "Doe"
)
user_1.save()
user_1.date_created = ModificationInformation(user = user_1.id)
user_1.date_modified = [ModificationInformation(user = user_1.id)]
user_1.save()