MongoEngine – Document Inheritance

MongoEngine – Document Inheritance

It is possible to define an inherited class of any user defined Document class. The inherited class may add extra fields if required. However, since such as a class is not a direct subclass of Document class, it will not create a new collection, instead its objects are stored in a collection used by its parent class. In the parent class, meta attribute ‘allow_inheritance the following example, we first define employee as a document class and set allow_inheritance to true. The salary class is derived from employee, adding two more fields dept and sal. Objects of Employee as well as salary classes are stored in employee collection.
In the following example, we first define Books as a document class and set allow_inheritance to true. The student class is derived from Books, adding two more fields stu_name and stu_contact. Objects of Books as well as student classes are stored in Books collection.

from mongoengine import connect
from mongoengine import StringField, IntField, Document, DecimalField
connect('mydata', host='mongodb://localhost/mydata')
class Books(Document):
book_id = IntField(unique=True, required=True)
book_name = StringField(max_length=50)
book_price = DecimalField()
meta = {'allow_inheritance': True}
def _init__(self, book_id, book_name, book_price):
self.book_id = book_id
self.book_name = book_name
self.book_price = book_price
class student(Books):
stu_name = StringField()
stu_contact = IntField()
def _init__(self, stu_name, stu_contact):
self.stu_name = stu_name
self.stu_contact = stu_contact
b1 = Books(book_id=1011, book_name='Operating System', book_price=2450)
b1.save()
s1 = student(book_id=1012, book_name='Web Technologies',
book_price=2450, stu_name='Deep', stu_contact=5566778899)
s1.save()

We can verify that two documents are stored in employee collection as follows −

{
"_id":{"$oid":"60349ebb4757ad01ab4288fc"},
"_cls":"Books",
"book_id":1011,
"book_name":"Operating System",
"book_price":2450
}
{
"_id":{"$oid":"6034a0011dc346bbb208723c"},
"_cls":"Books.student",
"book_id":1012,
"book_name":"Web Technologies",
"book_price":2450,
"stu_name":"Deep",
"stu_contact":{"$numberLong":"5566778899"}
}

Note that, in order to identify the respective Document class, MongoEngine adds a “_cls” field and sets its value as "Books" and "Books.student".
If you want to provide extra functionality to a group of Document classes, but without overhead of inheritance, you can first create an abstract class and then derive one or more classes from the same. To make a class abstract, meta attribute ‘abstract’ is set to True.

from mongoengine import connect
from mongoengine import StringField, IntField, Document, DecimalField
connect('mydata', host='mongodb://localhost/mydata')
class shape (Document):
meta={'abstract':True}
def area(self):
pass
class rectangle(shape):
width=IntField()
height=IntField()
def area(self):
return self.width*self.height
r1=rectangle(width=40, height=20).save()
MongoEngine – Advanced Queries (Prev Lesson)
(Next Lesson) MongoEngine – Atomic Updates
', { 'anonymize_ip': true });