on_form_prefill 如果执行 form.process 则删除所有字段值
on_form_prefill delete all fields values if it executes form.process
我想覆盖下面显示的 Matriline
模型的形式。
首先,我添加了一个额外的字段 clan
,然后在 on_form_prefill
中,我重置了该字段的选项和默认值(只是测试)。
没有调用 form.process
,选项设置为我的新选项,但未选择默认选项。
如果调用 form.process
,选项将设置为我的新选项并选择默认选项,但所有其他字段都被删除。
有人可以帮忙吗?
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'
def on_form_prefill(self, form, id):
form.clan.choices = [(1, "first"),(2,"second")]
form.clan.default = 2
form.process() # if commented, set choices but does not set default
# else set choices and default but delete all the other fields values (name and pod_id)
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
我有一个非常相似的问题,通过仅重新处理更改的字段使其工作。我还需要再次传递当前设置的值(否则它将始终设置为默认值):
from wtforms.utils import unset_value
class CustomView:
def on_form_prefill(self, form, id):
form.clan.choices = [(1, "first"),(2,"second")]
form.clan.default = 2
form.clan.process(None, form.clan.data or unset_value)
注意:由于 on_form_prefill
仅在 edit_view
中调用而不在 create_view
中调用,因此这仅适用于现有实例。
我通过process_data
解决了这个问题
def on_form_prefill(self, form, id):
form.clan.choices = [(1, "first"), (2, "second")]
form.clan.process_data(2)
希望对您有所帮助
我想覆盖下面显示的 Matriline
模型的形式。
首先,我添加了一个额外的字段 clan
,然后在 on_form_prefill
中,我重置了该字段的选项和默认值(只是测试)。
没有调用 form.process
,选项设置为我的新选项,但未选择默认选项。
如果调用 form.process
,选项将设置为我的新选项并选择默认选项,但所有其他字段都被删除。
有人可以帮忙吗?
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'
def on_form_prefill(self, form, id):
form.clan.choices = [(1, "first"),(2,"second")]
form.clan.default = 2
form.process() # if commented, set choices but does not set default
# else set choices and default but delete all the other fields values (name and pod_id)
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
我有一个非常相似的问题,通过仅重新处理更改的字段使其工作。我还需要再次传递当前设置的值(否则它将始终设置为默认值):
from wtforms.utils import unset_value
class CustomView:
def on_form_prefill(self, form, id):
form.clan.choices = [(1, "first"),(2,"second")]
form.clan.default = 2
form.clan.process(None, form.clan.data or unset_value)
注意:由于 on_form_prefill
仅在 edit_view
中调用而不在 create_view
中调用,因此这仅适用于现有实例。
我通过process_data
解决了这个问题def on_form_prefill(self, form, id):
form.clan.choices = [(1, "first"), (2, "second")]
form.clan.process_data(2)
希望对您有所帮助