如何在列表视图中设置额外字段的值? [英] How to set the value of an extra field in the list view?
本文介绍了如何在列表视图中设置额外字段的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 Matriline
类的 ModelView
中添加了一个额外的字段 clan
,并将其添加到列表视图的列中,如下所示
I added an extra field clan
to the ModelView
of the class Matriline
and add it to the columns in list view, as shown below.
如何在列表视图中设置额外字段 clan
的值?
How can I set the value of the extra field clan
in the list view?
views.py
class MatrilineAdmin(sqla.ModelView):
form_extra_fields = {
'clan': SelectField('Clan',
coerce=int,
choices=[ (c.id, c.name) for c in Clan.query.all()])
}
create_template = 'admin/create.html'
edit_template = 'admin/edit.html'
column_list = ('name', 'pod', 'clan')
models.py
class Matriline(db.Model):
__tablename__ = 'matriline'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(64))
calls = db.relationship('Call', backref='matriline', lazy='select')
pod_id = db.Column(db.Integer, db.ForeignKey('pod.id'))
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Pod(db.Model):
__tablename__ = 'pod'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(64))
matrilines = db.relationship('Matriline', backref='pod', lazy='select')
clan_id = db.Column(db.Integer, db.ForeignKey('clan.id'))
def __unicode__(self):
return self.name
def __str__(self):
return self.name
class Clan(db.Model):
__tablename__ = 'clan'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Unicode(64))
pods = db.relationship('Pod', backref='clan', lazy='select')
def __unicode__(self):
return self.name
推荐答案
根据 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')
这篇关于如何在列表视图中设置额外字段的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文