I want to use the widget presented in Admin page for Group creation in my Modelform (picture below) but I'm failing in do that.

Does anyone know how can I use a widget like this in my form?
I have two models that have a many-to-many relationship.
I have also created a ModelForm class to get all first_model objects that are related to my second_model.
You can define your field in the
ModelFormas aModelMultipleChoiceField(docs) which will hide most of the implementation details and output the exact same result.Here is an example of an actual implementation I've made in the past
Alternatively, if you want more control over how it's rendered, you can write the html yourself. Make sure to include the
multipleproperty in theselecttag: