问题描述:

I have a many to many link between Foos and Bars. I don't particularly like the multi select widget so was thinking of having a widget which allows selecting a single Bar and a button to add more choice fields.

From what I've been reading formsets may be the answer. Is it valid to have a formset made up of a form with only one choice field or is there a better way to get the behaviour I'm looking for?

网友答案:

I wouldn't worry about the quantity of fields in the form. If your 'child' model only has the one field, then I'd say it's perfectly valid to use an formset with single field forms in this application.

You should take a look at inline formsets, they should help with exactly what you need. Although I'm not 100% sure they work for M2M...

additional thought: If it doesn't inline forsmet doesn't work directly with M2M, you can just use a model formset, and manually save the relationship in your view after using formset.save(commit=False). docs: formset saving

相关阅读:
Top