Crispy 表单将 manytomanyfield 渲染为复选框

Crispy form render manytomanyfield as checkboxes

我有一个模型形式的 manytomany 对象,我想将其呈现为彼此下方的 select 字段,但无论我尝试什么,我都会让它们与脆皮 django 形式彼此相邻

class ContactForm(forms.ModelForm):
    choice = forms.ModelMultipleChoiceField(label=_('Request'),        widget=forms.CheckboxSelectMultiple(),required=False,
                                        queryset=ContactFormChoices.objects.all())
    name = forms.CharField(label=_('Name'))
    email = forms.EmailField(required=False, label=_('E-mail'))
    phone_number = forms.CharField(required=False, label=_('Phone number'))
    message = forms.CharField( widget=forms.Textarea , label=_('Message'))

    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'form-horizontal'
        self.helper.layout = Layout(
            Field('name', css_class='input-xlarge'),
            Field('email', css_class='input-xlarge'),
            Field('phone_number', css_class='input-xlarge'),
            Field('message', rows="3", css_class='input-xlarge'),
           #'choice',
            Field('choice'),
            FormActions(
                Submit('submit', _('Submit'), css_class="btn-primary")
            )

        )

    class Meta:
        model = ContactData
        fields = ['name','message','email','phone_number','choice']

和模型:

class ContactFormChoices(models.Model):
    '''
    The contact form options to show
    '''
    text = models.CharField(max_length=256)
    active = models.BooleanField(default=True)

    def __unicode__(self):
        return self.text

class ContactData(models.Model):
    '''
    The contact data (when customer fills in the contact form,
     this is mailed and saved here
    '''
    name = models.CharField(max_length=256,help_text=_("Name"))
    phone_number= models.CharField(max_length=256,null=True,blank=True,default=None)
    email = models.EmailField(max_length=256,null=True,blank=True,default=None)
    choice = models.ManyToManyField(ContactFormChoices,blank=True,default=None)
    message = models.TextField()

    def __unicode__(self):
        return self.name

看起来像这样:

有人有什么建议吗?

哇,经过大量的搜索和尝试....答案似乎很简单:

helper.layout = Layout(
        Field('name', css_class='input-xlarge'),
        Field('email', css_class='input-xlarge'),
        Field('phone_number', css_class='input-xlarge'),
        Field('message', rows="3", css_class='input-xlarge'),
        PrependedText('choice', ''),
        FormActions(
            Submit('submit', _('Submit'), css_class="btn-primary")
        )
    )

部分重复这个问题和答案:BooleanField checkbox not render correctly with crispy_forms using bootstrap