Integrating django-select2 (autocomplete) and django-dynamic-formset (dynamic formsets) -
Integrating django-select2 (autocomplete) and django-dynamic-formset (dynamic formsets) -
my app looks this:
forms.py:
class subform(forms.modelform): myfield = greatchoices() class meta: model = sub myformset = inlineformset_factory(my, sub, extra=1, form=subform) class greatchoices(automodelselect2field): queryset = great.objects
template.html
<form id="great" method="post" action="{% url "great-add" %}">{% csrf_token %} {{ form }} <h3>greats</h3> <table> <tbody> {% f in formset.forms %} <tr> <td> {% if f.instance.pk %}{{ f.delete }}{% endif %}</td> </tr> {% endfor %} </tbody> </table> {{ formset.management_form }} <input id="save_great" type="submit" value="add"> </form>
and template.js
$(document).ready(function() { $('form#recipe tbody tr').formset({ prefix: '{{ formset.prefix }}' });
this works - able add together single sub object formset, , submit form, , save()
s successfully.
however, if click "add another" when later click in sec input, cloned django-dynamic-formset, autocomplete first formset object activated. seems though select2 not beingness made aware new row has been added...
i have done bunch of reading , apparently formsets newish feature in select2: https://github.com/applegrew/django-select2/pull/127
i sense close making work , appreciate pointers.
i have similar setup using select2 straight instead of django app. had same issue django-dynamic-formset , prepare me modify jquery.formset.js
switch .clone(true)
, clone events, .clone(false)
clones construction leaves out jquery events hooked existing formset.
the alter simple, search lines these:
template = $('.' + options.formcssclass + ':last').clone(true).removeattr('id'); row = options.formtemplate.clone(true).removeclass('formset-custom-template'),
and alter .clone(true)
.clone(false)
:
template = $('.' + options.formcssclass + ':last').clone(false).removeattr('id'); row = options.formtemplate.clone(false).removeclass('formset-custom-template'),
django django-forms
Comments
Post a Comment