2014-12-07 37 views
0

我需要將單選按鈕更改爲我的應用程序中的複選框。Django ModelChoiceField將小部件更改爲複選框輸入標籤消失

event = forms.ModelChoiceField(
     queryset=Event.objects.all().order_by('-category', 'beginning_date'), 
     required=False, 
     widget=forms.CheckboxInput(), 
    ) 

我正在用小部件創建一個選擇字段,但標籤不見了?我如何修改它以顯示標籤?

回答

0

嘗試這樣:

event = models.ForeignKey(Event) 
在您的模型,並在您的形式

class <YourModel>Form(ModelForm): 
    class Meta: 
     model = <YourModel> 
     fields = '__all__' 
     widgets = { 'event': forms.CheckboxInput } 
+0

我有錯誤AttributeError的: '模塊' 對象有沒有屬性 'CheckboxChoiceInput' – ratata 2014-12-07 15:55:56

+0

是...在django> 1.4 CheckboxChoiceInput不存在。我使用CheckboxSelectMultiple並且沒關係,但也許你不想選擇多個值 – simopopov 2014-12-07 16:14:48

+0

在我的情況下,我在模型聲明中和模型聲明中有ManyToManyField,我將它改爲CheckboxSelectMultiple – simopopov 2014-12-07 16:18:20

相關問題