Mongoengine Document as EmbeddedDocument

I have a mongoengine.Document subclass User Defined.

class User(Document):
    meta = {'collection': 'users', 'allow_inheritance': False}
    _id = ObjectIdField()
    password = StringField(max_length=50)
    name = StringField(max_length=50, required

now I want to use it as a separate document with my own collection, but I want to also use it as an EmbeddedDocument as part of a game entry:

class Game(Document):
    meta = {'collection': 'games', 'allow_inheritance': False}
    _id = ObjectIdField()
    name = StringField()
    owner = EmbeddedField(User)

but there's a problem. Mongoengine allows you to embed only subclasses of EmbeddedDocument. Is there any way to solve this problem?

+5
source share
2 answers

Try it like this:

class User(Document, EmbeddedDocument):
    ...
+2
source

Aren't you using ReferenceField?

Otherwise, you save two detached but logically identical documents.

+1
source

All Articles