django:__ init__ def work但是没有以django形式更新到类

时间:2010-05-10 14:38:26

标签: django-forms

这是我的表格:

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

目的是,每次刷新网页时,都有新记录到数据库中的“构建”列。它应该更新到下拉框“build”,但它永远不会更新,除非重新启动服务器。我使用print并看到__init__检测到新记录但无法重新构建为Class。 非常感谢

1 个答案:

答案 0 :(得分:0)

您实际上需要更新self.fields['build']而不是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

因为您没有更新self.fields,所以您在编译时看到了查询的结果,而不是执行。