这真令人沮丧。
下面是我的文档定义,为了让子类文档继承自基类,我遵循以下建议: https : //groups.google.com/forum/#!topic / mongoengine-users / T8lCtGv_IYQ
车型/ thread.py
class Thread(object): meta = { 'allow_inheritance': True }车型/ forum.py
from models.thread import Thread from mongoengine import Document class ForumThread(Document, Thread): meta = { 'collection': 'forum_threads' }车型/ group.py
from models.thread import Thread from mongoengine import Document class GroupThread(Document, Thread): meta = { 'collection': 'group_threads' }在尝试这种事情的时候我得到了NotRegistered ;
>>> from models.forum import ForumThread >>> print ForumThread.objects().first() >>> NotRegistered: `Thread` has not been registered in the document registry. Importing the document class automatically registers it, has it been imported?任何帮助/建议将不胜感激。
This is really frustrating.
Below are my document definitions, in order to have sub-class documents inherit from a base class, I followed the suggestion here: https://groups.google.com/forum/#!topic/mongoengine-users/T8lCtGv_IYQ
models/thread.py
class Thread(object): meta = { 'allow_inheritance': True }models/forum.py
from models.thread import Thread from mongoengine import Document class ForumThread(Document, Thread): meta = { 'collection': 'forum_threads' }models/group.py
from models.thread import Thread from mongoengine import Document class GroupThread(Document, Thread): meta = { 'collection': 'group_threads' }I am getting the NotRegistered when attempting something of this sort;
>>> from models.forum import ForumThread >>> print ForumThread.objects().first() >>> NotRegistered: `Thread` has not been registered in the document registry. Importing the document class automatically registers it, has it been imported?Any help/advice would be much appreciated.
最满意答案
我不确定Thread是否需要meta ,或者尝试使用抽象基类[1]
class Thread(Document): meta = { 'abstract': True } class ForumThread(Thread): meta = { 'collection': 'forum_threads' } class GroupThread(Thread): meta = { 'collection': 'group_threads' }[1] http://docs.mongoengine.org/guide/defining-documents.html#abstract-classes
I'm not sure Thread needs a meta, alternatively try with an abstract base class[1]
class Thread(Document): meta = { 'abstract': True } class ForumThread(Thread): meta = { 'collection': 'forum_threads' } class GroupThread(Thread): meta = { 'collection': 'group_threads' }[1] http://docs.mongoengine.org/guide/defining-documents.html#abstract-classes
更多推荐
发布评论