[Answer]-Django – replace form widget for fields in list

1👍

You should do it in the __init__ constructor:

class CI_tableForm(ModelForm):

    class Meta:
        model = CI_table
        fields = report_query_values

    def __init__(self, *args, **kwargs):
        super(CI_tableForm, self).__init__(*args, **kwargs)
        for field in report_query_values:
            if "_id" in field:
                choices = self.fields[field].widget.choices
                self.fields[field].widget = forms.CheckboxSelectMultiple(
                                                             choices=choices)

Leave a comment