Mongoengine 简明教程

MongoEngine - Document Inheritance

可以为任何用户定义的文档类定义一个派生类。派生类可能会根据需要添加其他字段。但是,由于此类不是 Document 类的直接子类,因此它不会创建新集合,而是将其对象存储在父类使用的集合中。在父类中,meta 属性 ' allow_inheritance 在以下示例中,我们首先将 employee 定义为文档类,并将 allow_inheritance 设置为 true。salary 类是从 employee 派生的,它添加了另外两个字段 dept 和 sal。Employee 及 salary 类的对象存储在 employee 集合中。

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.

在以下示例中,我们首先将 employee 定义为文档类,并将 allow_inheritance 设置为 true。salary 类是从 employee 派生的,它添加了另外两个字段 dept 和 sal。Employee 及 salary 类的对象存储在 employee 集合中。

In 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.

from mongoengine import *
con=connect('newdb')
class employee (Document):
name=StringField(required=True)
branch=StringField()
meta={'allow_inheritance':True}
class salary(employee):
dept=StringField()
sal=IntField()
e1=employee(name='Bharat', branch='Chennai').save()
s1=salary(name='Deep', branch='Hyderabad', dept='Accounts', sal=25000).save()

我们可以验证 employee 集合中存储了两个文档,如下所示 −

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

{
"_id":{"$oid":"5ebc34f44baa3752530b278a"},
"_cls":"employee",
"name":"Bharat",
"branch":"Chennai"
}
{
"_id":{"$oid":"5ebc34f44baa3752530b278b"},
"_cls":"employee.salary",
"name":"Deep",
"branch":"Hyderabad",
"dept":"Accounts",
"sal":{"$numberInt":"25000"}
}

请注意,为了识别相应的文档类,MongoEngine 添加了一个 “_cls” 字段,并将它的值设置为 “employee” 和 “employee.salary”。

Note that, in order to identify the respective Document class, MongoEngine adds a “_cls” field and sets its value as "employee" and "employee.salary".

如果您想为一群文档类提供额外的功能,但没有继承的开销,那么您可以首先创建一个 abstract 类,然后从同一类派生一个或多个类。要使类抽象,将 meta 属性 'abstract' 设置为 True。

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 *
con=connect('newdb')

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=20, height=30).save()