Reputation: 33
Just like the answer,I want to dynamic Load the database in the choicefield, and i had do this
queue = forms.ChoiceField(label=u'queue',choices=((x.que,x.disr) for x in Queue.objects.all()))
but it doesn't work ,i must restart the server,the field can be update.
Upvotes: 1
Views: 1249
Reputation: 51988
You need to call the __init__
to load the data in form dynamically. For example:
class YourForm(forms.Form):
queue = forms.ChoiceField(label=u'queue')
def __init__(self, *args, **kwargs):
super(YourForm, self).__init__(*args, **kwargs)
self.fields['queue'].choices = ((x.que,x.disr) for x in Queue.objects.all()))
Reason for doing it is that, if you call __init__
in your form, it initializes an instance of a class and updates the choice list with latest data from database. For detail understanding, check here:Why do we use __init__ in python classes?
Upvotes: 3