Autocomplete allowing for custom entries and saving to manytomany-field
Django-newbie here: I am looking to employ a Facebook-style tokenfield
such as this one for Bootstrap or Select2's tag version as a replacement
for ModelMultipleChoiceField to use with a M2M-fields (I have about ~10 of
these) in a custom form (not in admin!).
I need the tokenfield to support two things:
Autocomplete for previous entries in the M2M table
Allow for entry of new items (that of course need to be saved to the M2M
table before the relationship is setup)
What I am struggling with is how to handle these new entries. I wonder
what the best practice is:
Alternative 1: Save new entries dynamically as they are entered (and thus
have the M2M entries ready before the form is submitted) and then loop
through and save after submit.
Alternative 2: Loop through the items in the field upon submit and match
them against existing ones in the M2M table (e.g. with get_or_create),
save and then set up the relationships.
Or — are there any even better ways of imagining this? Code samples are
greatly appreciated.
No comments:
Post a Comment