Django ManytoManyField and widgets

user3541631 picture user3541631 · Nov 5, 2017 · Viewed 8.7k times · Source

I have two models, Product and Category and ManytoMany field in Product. The Category appear as key on ProductCreate View.

I need to customize the widget and field for Categories.

I checked in Django source Fields and Widgets but I don't see a reference(class) for ManyToMany.

To what type of Field and Widget ManyToMany relationship corresponds(I presume is Charfield as save or SelectField)? Where I can find the code ? (an example to customize field/widget in this case)

Answer

anjaneyulubatta505 picture anjaneyulubatta505 · Nov 5, 2017

A model ManyToManyField is represented as a MultipleChoiceField and the default widget is SelectMultiple But, we can customise it. You can find it in below references.
[1]https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#field-types
[2]https://docs.djangoproject.com/en/dev/ref/forms/widgets/#setting-arguments-for-widgets