5
votes

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 standalone document with its own collection but i want also to use it as EmbeddedDocument as part of Game record:

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

but there is a problem. Mongoengine allows to embed only EmbeddedDocument subclasses. Is there a way to solve it?

2

2 Answers

2
votes

Try like this:

class User(Document, EmbeddedDocument):
    ...
1
votes

Shouldn't you be using a ReferenceField?

Otherwise, you're storing two disconnected, but logically identical, documents.