我有一个可以代表多种类型数据的集合:
class Taxes(db.Document):
meta = {'collection': 'taxes'}
type = db.StringField() # State, local, federal
owner = db.ReferenceField(User, unique=True)
name = db.StringField()
fiscal_year = db.IntField()
我想要做的是拥有DynamicEmbeddedDocument
或将其设为DynamicDocument
以容纳不同的模型。
例如:
class Taxes(db.Document):
...
# This is made up syntax
data = db.EmbeddedDocumentField(StateTaxes, LocalTaxes, FederalTaxes)
或者:
class Taxes(db.DynamicDocument):
...
class StateTaxes(Taxes):
state_name = db.StringField()
class LocalTaxes(Taxes):
locality_name = db.StringField()
目标是这样做:
# Embedded Dynamic Document example
taxes = Taxes.objects(owner=current_user).all()
state_taxes = [tax.data for tax in taxes if tax.type == 'state']
state_names = [tax_data.state_name for tax_data in state_taxes]
# Dynamic Document example
taxes = Taxes.objects(owner=current_user).all()
state_taxes = [tax for tax in taxes if tax.type == 'state']
state_names = [tax.state_name for tax in state_taxes]
备注:
**这些是我不使用单独收藏品的主要原因
这可能吗?
答案 0 :(得分:1)
您可以创建一个涵盖所需的所有基本属性(字段)和方法的基类。例如:
class BaseTaxes(db.Document):
name = db.StringField()
value = db.IntegerField()
meta = {'allow_inheritance': True}
def apply_tax(self, value):
return value*(1+self.value)
使用此基类,您可以创建不同的版本:
class StateTaxes(BaseTaxes):
state = db.StringField()
因此,StateTaxes类继承了BaseTaxes及其方法的两个属性(更多细节here)。因为它继承了BaseTaxes类,所以它将保存在同一个集合(BaseTaxes)中,并且查询可以到达所有子类:
results = BaseTaxes.objects().all()
然后,按子类分割结果:
state_taxes = [item for item in results if isinstance(item,StateTaxes)]