tags:

views:

47

answers:

1

Hi expert there, this is my form:

class IPTrackerSearchForm(forms.Form):
 keyword = forms.CharField(max_length=100, widget=forms.TextInput(attrs={'size':'50'}))
 search_in = forms.ChoiceField(required=False, choices=ANY_CHOICE + MODULE_SEARCH_IN_CHOICES)
 product       = forms.CharField(max_length=64,widget=forms.TextInput(attrs={'size':'50'})) 
 family       = forms.CharField(max_length=64,widget=forms.TextInput(attrs={'size':'50'})) 
 temp_result = Merlin.objects.values('build').distinct()
 result = [(value['build'], value['build']) for value in temp_result]
 build       = forms.ChoiceField(choices=ANY_CHOICE + result)

 circuit_name     = forms.CharField(max_length=256,widget=forms.TextInput(attrs={'size':'50'})) 
 parameterization    = forms.CharField(max_length=1024,widget=forms.TextInput(attrs={'size':'50'}))  
 metric       = forms.CharField(max_length=64,widget=forms.TextInput(attrs={'size':'50'})) 

 show_in_one_page = forms.BooleanField(required=False, label="Show filtered result in one page", widget=forms.CheckboxInput(attrs={'class':'checkbox'}))
 def __init__(self, *args, **kwargs): 
  super(IPTrackerSearchForm, self).__init__(*args, **kwargs)
  temp_result = Merlin.objects.values('build').distinct()
  self.result = [(value['build'], value['build']) for value in temp_result]
  self.build       = forms.ChoiceField(choices=ANY_CHOICE + self.result)
  print self.result

With the purpose that, each time I refresh the webpage, when have new record to "build" column in database. It should update to the drop down box "build" here but It never update unless restart the server. I use print and see that __init__ detect new record but can not refect to build in Class. Many thanks

A: 

You actually need to update self.fields['build'] instead of self.build.

 def __init__(self, *args, **kwargs): 
  super(IPTrackerSearchForm, self).__init__(*args, **kwargs)
  temp_result = Merlin.objects.values('build').distinct()
  result = [(value['build'], value['build']) for value in temp_result]
  self.fields['build']  = forms.ChoiceField(choices=ANY_CHOICE + result)
  print result

Because you're not updating self.fields, you are seeing the result of the query at compile time, not execution.

Jack M.
Hi Jack M. It works. :) so happy, thanks a lot. Many thanks.You are a manthanks