2014-09-10 90 views
3

我需要的是下面給出的formset中每個表單的單選按鈕。django嵌入式formset的單選按鈕

class AppvideoDemoForm(forms.ModelForm): 

    class Meta: 
     model = AppvideoDemo 

    def __init__ (self, *args, **kwargs): 
     super(AppvideoDemoForm, self).__init__(*args, **kwargs) 
     self.fields['active'] = forms.BooleanField(widget = forms.RadioSelect(choices=((self.prefix, 'Set this as primary'),))) 

    def add_prefix(self, field): 
     if field == 'active': 
      return field 
     else: 
      return self.prefix and ('%s-%s' % (self.prefix, field)) or field 

AppvideoDemoFormSet = inlineformset_factory(Applications, AppvideoDemo, extra=3,form=AppvideoDemoForm,formset=MandatoryInlineFormSet, can_delete=False) 

但是,當我嘗試自己的方式時發現單選按鈕是不可選擇和多選的。

回答

0

代表無線電按鈕,您需要使用checkboxfield whith radioselect小部件。

choices = (('0', 'LORENZO_LAMAS'), (1, 'EL RENEGADO')) 
filters = forms.ChoiceField(required=True, 
          widget=forms.RadioSelect(attrs={'class': ''}), 
          choices=choices) 

我希望這個解決你的問題

+0

我想你是不是清楚我的問題。我有一個形式爲AppVideoDemoForm的inlineformset,它具有一個布爾型字段'active'。需要將formset中的任意一個表單設置爲活動狀態。 – anoop 2014-09-10 12:55:59