表单数据不刷新与最新条目 [英] Form Data not refreshing with newest entries

查看:201
本文介绍了表单数据不刷新与最新条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在客户表中添加一个新客户并访问AddOrderForm表单时,我没有得到新客户的选择。但在服务器重新启动时,我能够在选择列表中获得新客户。任何原因? / p>

客户表

 类Customer(db.Model):
id = db.Column(db.Integer,primary_key = True)
email = db.Column(db.String(120),unique = True)
mobile_num = db.Column(db.String (13),unique = True)
name = db.Column(db.String(120))
marketing_source = db.Column(db.String(120))
date_of_birth = db。列(db.DateTime)
性别= db.Column(db.String(13))
store_id = db.Column(db.Integer,db.ForeignKey('store.id'))

def __init __(self,email,mobile_num,name,marketing_source,date_of_birth,gender,store_id):
self.email =电子邮件
self.mobile_num = mobile_num
self。 name = name
self.marketing_source = marketing_so urce
self.date_of_birth = date_of_birth
self.gender = gender
self.store_id = store_id
def __repr __(self):
return'%r,%s' %(self.name.encode('utf-8'),self.mobile_num)

AddOrderForm

  class AddOrderForm(Form):
order_id = TextField('Website Order Id',[validators.length(min = 4,max = 120)])
item_name = TextField('Item Name',[validators.length(min = 4,max = 120)])
item_cost = DecimalField('Item Cost' ('Customer',coerce = int,choices = convert_list_wtforms_choices(Customer.query.all()))
order_category = SelectField('Order Category',coerce = int,choices = [(1,'Mobiles'),(2,'Clothing')])
linq_shipping_cost = DecimalField('Linq Shipping Cost',[validators.Required()])
website_shipping_cost = DecimalField('Website Shipping成本',[validators.Required()])
advance_amount = DecimalField('Advance Amount',[validators.Required()])
website = SelectField('Website',coerce = int,choices = [(1,'Amazon'),(2,'Flipkart')])
other = TextField('其他信息')

在从视图中访问表单时,我不会在custmer_id选项中获得最新添加的客户。任何想法来解决这个问题?

解决方案

当您定义表单时,您只设置一次选择。
$ b

class AddOrderForm(Form):
customer_id = SelectField('Customer',coerce = int)
$ b $ pre $ def $ __init __(self,* args,** kwargs):
super(AddOrderForm,self).__ init __( * args,** kwargs)
self.customer_id.choices = convert_list_wtforms_choices(Customer.query.all())


When I add a new customer in customer table and access AddOrderForm form, I don't get the new customer in the choices.But on server restart I am able to get the new customer in the choices list.Any reason ?

Customer Table

class Customer(db.Model):
     id = db.Column(db.Integer, primary_key=True)
     email = db.Column(db.String(120), unique=True)
     mobile_num = db.Column(db.String(13), unique=True)
     name = db.Column(db.String(120))
     marketing_source = db.Column(db.String(120))
     date_of_birth = db.Column(db.DateTime)
     gender = db.Column(db.String(13))
     store_id = db.Column(db.Integer, db.ForeignKey('store.id'))

     def __init__(self,email,mobile_num,name,marketing_source,date_of_birth, gender,store_id):
         self.email = email
         self.mobile_num = mobile_num
         self.name = name
         self.marketing_source = marketing_source
         self.date_of_birth = date_of_birth
         self.gender = gender
         self.store_id = store_id
     def __repr__(self):
          return '%r, %s ' % (self.name.encode('utf-8'), self.mobile_num)

AddOrderForm

class AddOrderForm(Form):
    order_id  = TextField('Website Order Id', [validators.length(min=4, max=120)])
    item_name = TextField('Item Name', [validators.length(min=4, max=120)])
    item_cost = DecimalField('Item Cost' , [validators.Required()])
    custmer_id = SelectField('Customer',coerce=int,choices= convert_list_wtforms_choices(Customer.query.all()))
    order_category = SelectField('Order Category',coerce=int,choices=[(1,'Mobiles'), (2,'Clothing')])
    linq_shipping_cost =  DecimalField('Linq Shipping Cost' , [validators.Required()])
    website_shipping_cost = DecimalField('Website Shipping Cost' , [validators.Required()])
    advance_amount = DecimalField('Advance Amount' , [validators.Required()])
    website = SelectField('Website', coerce=int,choices=[(1,'Amazon'), (2,'Flipkart')] )
    other = TextField('Any Other Information')

While accessing the form from a view I don't get the latest added customer in the custmer_id choices. Any idea to fix this?

解决方案

You're only setting the choices once, when you define the form. Instead, re-select them every time you instantiate the form.

class AddOrderForm(Form): customer_id = SelectField('Customer', coerce=int)

def __init__(self, *args, **kwargs):
    super(AddOrderForm, self).__init__(*args, **kwargs)
    self.customer_id.choices = convert_list_wtforms_choices(Customer.query.all())

这篇关于表单数据不刷新与最新条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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