脆皮表格:可以更好地控制CheckboxSelectMultiple

时间:2019-02-25 11:41:09

标签: django django-forms django-crispy-forms

我正在使用Cripsy表单,并具有一个使用CheckboxSelectMultiple的ModelMultipleChoiceField。但是,这些复选框在标准复选框处呈现,我需要在复选框字段(“ custom-control-input”)中添加一些CSS,以使其像我的其余其余自举复选框一样呈现。

关于从哪里开始的任何想法?

class CustomPermissionMultipleChoiceField(forms.ModelMultipleChoiceField):
    def label_from_instance(self, obj):
        return "%s" % obj.name


class UserPersonalInfoEdit(forms.ModelForm):
    permission_codename_list = (
        'can_view_admin', 'change_retailer', 'change_website', 'change_rating', 'change_contract',
        'view_contract',
        )
    user_permissions = CustomPermissionMultipleChoiceField(
        required=False, widget=forms.CheckboxSelectMultiple,
        queryset=Permission.objects.filter(codename__in=permission_codename_list))
    class Meta:
        model = CustomUser
        fields = ['first_name', 'last_name', 'email', 'username', 'user_permissions']

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_method = 'POST'
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True
        self.fields['email'].required = True
        self.fields['username'].help_text = None
        self.fields['is_active'].help_text = None
        self.helper.layout = Layout(
            Row(
                Column('first_name', css_class='form-group col-md-4 mb-0'),
                Column('last_name', css_class='form-group col-md-4 mb-0'),
                Column('username', css_class='form-group col-md-4 mb-0'),
                ),
            Row(
                Column('email', css_class='form-group col-md-4 mb-0'),
                ),
            HTML('<hr>'),
            Row(
                Column('user_permissions', css_class='form-group col-md-4 mb-0'),
                ),
            HTML('<hr>'),
            Submit('submit', 'Save'),
            HTML('<a class="btn btn-primary" href="{% url "core:user_list" %}" role="button">Close</a>')
            )

0 个答案:

没有答案