J-bob
J-bob

Reputation: 9126

MongoEngine: Documents disappear after allowing_inheritance

I have a MongoEngine Document that previously didn't allow inheritance. I then wanted to inherit from it, so I set {allow_inheritance:True}. As soon as I did that the existing documents for that model didn't appear anymore when calling <myModel>.objects. If I momentarily set {allow_inheritance:False} then the documents come back. Why would that be?

I'm using MongoEngine verison 0.8.7

Upvotes: 1

Views: 341

Answers (1)

J-bob
J-bob

Reputation: 9126

Figured it out. When using allow_inheritance, MongoEngine stores a special _cls field in the base document with the name of the class, or derived class. So for your BaseClass it would store "BaseClass" as the value, and for your DerivedClass it would would store "BaseClass.DerivedClass" as the value. But without allow_inheritance set initially, it does not have this special _cls field set. So after setting allow_inheritance I had to go in to the mongo field manually (not through mongoengine) and perform an update to add the _cls field with the BaseClass value and then documents reappeared.

Upvotes: 1

Related Questions