[Fixed]-ModelMultipleChoiceField' object has no attribute 'to_field_name'


The widgets dictionary expects the values to be widget instances such as TextArea(), TextInput(), etc.

If you want to use forms.ModelMultipleChoiceField, you could do something like this

class DrugiForm(ModelForm):
    kids = forms.ModelMultipleChoiceField(queryset=Pierwszy.objects.none(), widget=forms.CheckboxSelectMultiple())

    def __init__(self, *args, **kwargs):
        super(DrugiForm, self).__init__(*args, **kwargs)
        instance = getattr(self, 'instance', None)
        if instance and instance.pk:
        if instance.name is not None:
            self.fields['name'].widget.attrs['readonly'] = True

    class Meta:
        model = Drugi
        fields = ('ext_name','name','kids','extra')

Leave a comment