How can I create objects for a ManyToMany fields in the same form in Django?

So Django model forms make a multiple choice field out of a many to many field in the model. What I would like to do is to have a plus button similar to that of the admin site, that prompts you with a modal to create a new object for the many-to-many field, instead of just being able to select from the queryset of existing ones. Is mtm the right relation to use? Any pointers on how to achieve this?

An example from the admin page where questions is a manytomany field in the Survey model

Short anwer, use ajax in your template. It will allow you to add a new object without refreshing your page. If you search stack overflow there will be a lot of examples of how to implement ajax.

Back to Top