Mongoengine:使文档从EmbeddedDocuments保持最新

时间:2018-11-07 11:52:52

标签: python mongoengine blinker

我有一个模特:

df['YearType'] = np.where(df['year'] == 1995, 'Value1995', 'ValueRetired')

res = df.pivot(index='ID', columns='YearType', values='Value')

print(res)

YearType  Value1995  ValueRetired
ID                               
3               200           100
4               300           250
5               150            95

还有一个模式

class Portfolio(db.Document):
    user = db.ReferenceField(User)

    total_value = db.FloatField(required=True)

    currency = db.StringField(required=True)
    units = db.FloatField(required=True)
    usd_value = db.FloatField(required=True)

    meta = {
        'indexes': ['user']
    }

    def __init__(self, **kwargs):
        super(Portfolio, self).__init__(**kwargs)

    def __repr__(self):
        return '(Portfolio : %s - %s)' % self.action, self.status]

任何人都可以建议以最佳方式建议将class ObjectId(fields.Field): def _serialize(self, value, attr, obj): if value is None: return '' return str(value) class PortfolioSchema(ma.Schema): id = ObjectId(dump_only=True) user = ma.Nested(UserSchema) total_value = ma.Float(required=True) currency = ma.String(required=True) units = ma.Float(required=True) usd_value = db.Float(required=True) class Meta: model = Portfolio @post_load def make_portfolio(self, data): return Portfolio(**data) 作为投资组合上的Embed,同时确保文档Embedded Document是变量的总和。 total_value的数量:Embedded

任何帮助表示赞赏。

0 个答案:

没有答案