4

我正在使用带有 mongoengine 0.8.4 的 pymongo 版本 2.6.1。我想使用给出的教程创建一个代码

链接https://mongoengine-odm.readthedocs.org/en/latest/tutorial.html

我的代码在 sample.py 文件中如下:

from mongoengine import *
import datetime

class BlogPost(Document):
    title = StringField(required=True, max_length=200)
    posted = DateTimeField(default=datetime.datetime.now)
    tags = ListField(StringField(max_length=50))

class TextPost(BlogPost):
    content = StringField(required=True)

class LinkPost(BlogPost):
    url = StringField(required=True)

在我运行的终端 wnen 上,出现以下错误:

Python 2.7.3(默认,2013 年 4 月 10 日,05:46:21)[GCC 4.6.3] 在 linux2 上键入“帮助”、“版权”、“信用”或“许可”以获取更多信息。

从示例导入 Post Traceback(最近一次调用最后):文件“”,第 1 行,在文件“sample.py”中,第 9 行,在类 TextPost(BlogPost)中:文件“/usr/local/lib/python2.7/ dist-packages/mongoengine/base/metaclasses.py”,第 332 行,在的 new_class = super_new(cls, name, bases, attrs) 文件“/usr/local/lib/python2.7/dist-packages/mongoengine/base /metaclasses.py",第 120 行,在的 基础上。name ) ValueError: Document BlogPost 不能被子类化

请帮我。我也试过卸载并重新安装。但它不起作用。

4

1 回答 1

18

查看文档,您似乎需要:

meta = {'allow_inheritance': True}

在你的BlogPost课上。

于 2013-09-06T11:53:09.513 回答