Remi-007
Remi-007

Reputation: 145

Django: forms.ChoiceField doesnt refresh after new page loading

In my Django App, I wrote a form with a forms.ChoiceField . The choice should be a list of items that are changing every couple of minutes in my DB. I would like to have the current list of items in a drop-down button when I reload the page.

My code works good except the forms.ChoiceField does not update. To update I have to restart the Django server.

I don't know what i am missing, Can you help me ? It must be something small.

from 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')])}

from the template

<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;"     > 

Tank you in Adavance

Upvotes: 0

Views: 1513

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599610

No, anything defined at class level will only be evaluated once, when the class is first imported.

You could do this in the __init__ method, but a better approach is to use the field that is meant for getting choices from querysets: ModelChoiceField.

Upvotes: 3

Related Questions