Rob
Rob

Reputation: 3479

Allow Inheritance in Meta of Document

If I have a Document:

class First(Document):
    field1 = StringField()
    meta = {'allow_inheritance': True}

class Second(First):
    field2 = StringField()
    meta = {
        'indexes': ['field2']
    }

Will this work? I'm curious if the meta in Second will overwrite the allow_inheritance in First and will break the app. It seems to work in testing but I'm not sure how this is handled.

Upvotes: 1

Views: 1165

Answers (1)

sergiuz
sergiuz

Reputation: 5529

Yes it will work.

meta is not overriden, you can say it will be updated.

When you instantiate a document from Second model, and save() it, will be saved in the first collection, and in that moment in first collection will be created the field2 index.

More Details:

from the source code, The class Document got a metaclass=TopLevelDocumentMetaclass which change the default action override to update, something like attrs["_meta"].update(attrs.get("meta", {}))

Upvotes: 1

Related Questions