我从数据库中获得了用于列出客户的表格。
class CustomerForm(forms.Form):
customer = forms.ChoiceField(choices=[], required=True, label='Customer')
def __init__(self, *args, **kwargs):
super(CustomerForm, self).__init__(*args, **kwargs)
self.fields['customer'] = forms.ChoiceField(choices=[(addcustomer.company_name + ';' + addcustomer.address + ';' + addcustomer.country + ';' + addcustomer.zip + ';' + addcustomer.state_province + ';' + addcustomer.city,
addcustomer.company_name + ' ' + addcustomer.address + ' ' + addcustomer.country + ' ' + addcustomer.zip + ' ' + addcustomer.state_province + ' ' + addcustomer.city) for addcustomer in customers])
Run Code Online (Sandbox Code Playgroud)
接下来,我得到一个模态窗口,里面有一个“添加客户”表单。
问题:当我通过模式表单将新客户插入数据库(实际上正在运行)时,在重新启动本地服务器之前,CustomerForm不会是它的选择。
我需要一种在添加客户后尽快更新列表的方法。尝试过这种__init__方法,但没有运气。