2012-02-09 8 views
1

私のフォームの1つはModelMultipleChoiceFieldを使用しており、HTML上にチェックボックスのリストを表示しています。Django:フォームのModelMultipleChoiceFieldのCSS IDを変更するにはどうすればよいですか?

class CreateProfileForm(forms.Form): 
    first_name = forms.CharField(label='First Name', max_length=50) 
    last_name = forms.CharField(label='Last Name', max_length=50) 
    topics = forms.ModelMultipleChoiceField(label='Assign Topic(s)', 
      widget=CheckboxSelectMultiple(), 
      queryset=None) 

    def __init__(self, *args, **kwargs): 
     event = kwargs.pop('event') 
     super (CreateProfileForm, self).__init__(*args, **kwargs) 
     self.fields['topics'].queryset = Topic.objects.filter(event=event) 

HTML出力

<div id="div_topics"> 
    <label class="fields"> 
     <strong><label for="id_topics_0">Assign Topic(s)</label></strong> 
    </label> 
    <ul> 
<li><label for="id_topics_0"><div class="checker" id="uniform-id_topics_0"><span><input type="checkbox" name="topics" value="3" id="id_topics_0" style="opacity: 0; "></span></div> Size</label></li> 
<li><label for="id_topics_1"><div class="checker" id="uniform-id_topics_1"><span><input type="checkbox" name="topics" value="2" id="id_topics_1" style="opacity: 0; "></span></div> Flavour</label></li> 
<li><label for="id_topics_2"><div class="checker" id="uniform-id_topics_2"><span><input type="checkbox" name="topics" value="1" id="id_topics_2" style="opacity: 0; "></span></div> Citrus</label></li> 
</ul> 
</div> 

にはどうすればinput type要素のidの値を変更できますか?または、基本的にid_topics_0などを別のものに変更しますか?

答えて

1

この

def __init__(self, *args, **kwargs): 
    event = kwargs.pop('event') 
    super (CreateProfileForm, self).__init__(*args, **kwargs) 
    self.fields['topics'].queryset = Topic.objects.filter(event=event) 
    self.fields['topics'].widget.attrs['class'] = "new_topics_class" 
をお試しください
関連する問題