`NotRegistered`使用多重继承时的异常(`NotRegistered` Exception when using multiple inheritance)

编程入门 行业动态 更新时间:2024-10-26 02:26:08
`NotRegistered`使用多重继承时的异常(`NotRegistered` Exception when using multiple inheritance)

这真令人沮丧。

下面是我的文档定义,为了让子类文档继承自基类,我遵循以下建议: 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

更多推荐

本文发布于:2023-07-17 07:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1141188.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:异常   NotRegistered   Exception   inheritance   multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!