表单数据未使用最新条目刷新

时间:2016-08-23 18:48:52

标签: python flask flask-sqlalchemy flask-wtforms

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

客户表

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')

从视图访问表单时,我没有在custmer_id选项中获得最新添加的客户。有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

在定义表单时,您只需设置一次选项。相反,每次实例化表单时都要重新选择它们。

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())