Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error : "Document BlogPost may not be subclassed" mongoengine

I am using pymongo version 2.6.1 with mongoengine 0.8.4. And I want to create one code using tutorial given in

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

My code is as follows in sample.py file:

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)

On terminal wnen I run , I get follwing error:

Python 2.7.3 (default, Apr 10 2013, 05:46:21) [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information.

from sample import Post Traceback (most recent call last): File "", line 1, in File "sample.py", line 9, in class TextPost(BlogPost): File "/usr/local/lib/python2.7/dist-packages/mongoengine/base/metaclasses.py", line 332, in new new_class = super_new(cls, name, bases, attrs) File "/usr/local/lib/python2.7/dist-packages/mongoengine/base/metaclasses.py", line 120, in new base.name) ValueError: Document BlogPost may not be subclassed

Please help me. I also tried uninstall and reinstall. but it not works.

like image 542
Swapnil Farande Avatar asked Dec 02 '22 18:12

Swapnil Farande


1 Answers

Looking at the docs, it seems that you need:

meta = {'allow_inheritance': True}

in your BlogPost class.

like image 53
Martin Maillard Avatar answered Feb 02 '23 00:02

Martin Maillard