[Answer]-How to change the ManyToManyField widget?

1๐Ÿ‘

I know the question is 2 years old but trying to answer for others who are looking for something similar.

You can use a widget similar to ManyToManyField, SelectMultiple but just mark it as readonly and disabled. Marking these as disabled or readonly within __init__ in forms may not work. It should be done as part of Meta widgets sections.

class ParticipantInlineForm(forms.ModelForm):
    class Meta:
        model = Participant
        widgets = {
            'persons': forms.SelectMultiple(attrs={'readonly': 'True', 'disabled': 'True'})
        }

Make sure __str__ has been implemented to a meaningful text in Persons class

Since it is disabled and marked as readonly, the value cannot be changed but it displays proper string values.

๐Ÿ‘คsavp

Leave a comment