为了账号安全,请及时绑定邮箱和手机立即绑定

Django:forms.ChoiceField 在新页面加载后不会刷新

Django:forms.ChoiceField 在新页面加载后不会刷新

精慕HU 2021-07-19 17:05:51
在我的 Django 应用程序中,我写了一个带有forms.ChoiceField的表单 。选择应该是我的数据库中每几分钟更改一次的项目列表。当我重新加载页面时,我希望在下拉按钮中显示当前的项目列表。我的代码运行良好,除了 forms.ChoiceField 不更新。要更新,我必须重新启动 Django 服务器。我不知道我错过了什么,你能帮我吗?一定是小东西。来自forms.pyclass BookingForm(forms.ModelForm):    make_list_of_tuple = lambda list_machine  :  [tuple( [i,i]) for i in list_machine]    MACHINES= tuple( QA_machine_DB.objects.values_list('QAmachine',flat=True))    CHOICE_QA_MACHINES= make_list_of_tuple(MACHINES)    QAmachine= forms.ChoiceField(choices=CHOICE_QA_MACHINES)    class Meta():        model= QA_machine_DB        fields= ['QAmachine', 'owner', 'comments','status']        # http://nanvel.name/2014/03/django-change-modelform-widget        widgets = {                   'owner':forms.TextInput(attrs={'placeholder':'owner'}),                   'comments':forms.TextInput(attrs={'placeholder':'comments'}),                   'status': forms.RadioSelect( choices=[('busy','busy'),('free','free')])}从模板<form   class="form-group" method="post" novalidate >    {% csrf_token %}     <table >       <td>          {{ BookingForm.QAmachine}}        </td>         <td>           {{ BookingForm.owner.errors }}           {{ BookingForm.owner}}         </td>         <td>           {{ BookingForm.comments.errors }}           {{ BookingForm.comments}}         </td>         <td>           {% for radio in BookingForm.status %}           {{ radio }}           {% endfor %}         </td>    </table>  <input type="submit" class="btn btn-success" value="submit status change" style="float: right;"     > 坦克你在Advance
查看完整描述

1 回答

?
杨__羊羊

TA贡献1943条经验 获得超7个赞

不,在类级别定义的任何内容只会在第一次导入类时评估一次。

您可以在__init__方法中执行此操作,但更好的方法是使用用于从查询集中获取选择的字段:ModelChoiceField


查看完整回答
反对 回复 2021-07-21
  • 1 回答
  • 0 关注
  • 310 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信