on_form_prefill删除所有的字段值,如果它执行form.process [英] on_form_prefill delete all fields values if it executes form.process

查看:251
本文介绍了on_form_prefill删除所有的字段值,如果它执行form.process的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想重写下面显示的 Matriline 模型的形式。

首先我添加一个额外的字段 clan on_form_prefill 我重置这个字段的选项和默认值(只是测试)。

在不调用 form.process 的情况下,选项被设置为我的新选项,但默认选项未被选中。 b

如果调用 form.process ,则将选项设置为我的新选项,并选择默认选项,但是删除所有其他字段。 / p>

任何人都可以帮忙吗?

views.py

  class MatrilineAdmin(sqla.ModelView):
$ b $ 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()#如果注释,设置选项,但不设置默认值
#else设置选项和默认值,但删除所有其他字段值(名称和pod_id)

p>

models.py

 <$ c $ (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
$ b def __str __(self):
return self.name


class Pod(db.Model):
__tablename__ ='pod'
id = db.Column(db.Integer,primary_key = True)
name = db.Col umn(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

$ b $ 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


解决方案

我通过 process_data 来解决这个问题。 p>

  def on_form_prefill(self,form,id):
form.clan.choices = [(1,first) ,(2,second)]
form.clan.proce ss_data(2)

我希望这可以帮助你


I want to override the form of the Matriline model shown below.

First I add an extra field clan, and in on_form_prefill I reset this field's choices and default (just testing).

Without calling form.process the choices are set to my new options, but the default option is not selected.

If calling form.process the choices are set to my new options and the default option is selected, but all the other fields are deleted.

Anybody can help?

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

解决方案

I solve this problem by process_data

def on_form_prefill(self, form, id):
    form.clan.choices = [(1, "first"), (2, "second")]
    form.clan.process_data(2)

I hope this can help you

这篇关于on_form_prefill删除所有的字段值,如果它执行form.process的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆