且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在列表视图中设置额外字段的值?

更新时间:2022-04-18 22:38:25

根据 MatrilineAdmin -class中覆盖 get_list 方法.

According to the code in BaseModelView.index_view there is no "configuration" option for that. You will need to override the get_list method in your MatrilineAdmin-class.

这看起来像:

class MadrilineAdmin(sqla.ModelView):
    def get_list(self, *args, **kwargs):
        count, data = super().get_list(*args, **kwargs)
        for d in data:
            d.clan = 'whatever you want'
        return count, data


您还可以尝试指定从母体到氏族的关系并直接在模型类中定义clan属性:


You could also try to specify a relationship from Matriline to Clan and to define the clan property directly in the model class:

class Matriline(db.Model):
    # …
    pod = db.relationship('Pod')

    @property
    def clan(self):
        if not self.pod:
            return None
        return self.pod.clan

class Pod(db.Model):
    # …
    clan = db.relationship('Clan')