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

在我的 Django 应用程序中,我写了一个带有forms.ChoiceField的表单 。选择应该是我的数据库中每几分钟更改一次的项目列表。当我重新加载页面时,我希望在下拉按钮中显示当前的项目列表。


我的代码运行良好,除了 forms.ChoiceField 不更新。要更新,我必须重新启动 Django 服务器。


我不知道我错过了什么,你能帮我吗?一定是小东西。


来自forms.py


class 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


精慕HU
浏览 337回答 1
1回答

杨__羊羊

不,在类级别定义的任何内容只会在第一次导入类时评估一次。您可以在__init__方法中执行此操作,但更好的方法是使用用于从查询集中获取选择的字段:ModelChoiceField。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python