djangodjango-select2

Trying to combine Django inline-formset, django-select2 and django-dynamic-formset


I am trying to combine all mentioned in the title with always something missing. I've gone through all possible posts that I could find and still am not sure what to do.

So I basically have two models, which are master-detail. When I use plain django stuff - everything is OK, but in detail model I have more than 10000 elements, and classic select field is impossible to use. So I switched to select2 through django-select2 module. I somehow managed to put everything together, but then had to add django-dynamic-formset to be able to dynamically add new rows if I need more than three that django renders by default. Again, I managed to make everything work, had some issues while I put everything together, but now I have two issues.

If I leave this peace of code in jquery.formset.js:

insertDeleteLink(row);

I will get this message:

jquery.formset.js:67 Uncaught ReferenceError: deleteButtonHTML is not defined
    at insertDeleteLink (jquery.formset.js:67)
    at HTMLTableRowElement.<anonymous> (jquery.formset.js:146)
    at Function.each (jquery-2.2.4.min.js:2)
    at n.fn.init.each (jquery-2.2.4.min.js:2)
    at n.fn.init.$.fn.formset (jquery.formset.js:124)
    at HTMLDocument.<anonymous> (1:92)
    at i (jquery-2.2.4.min.js:2)
    at Object.fireWith [as resolveWith] (jquery-2.2.4.min.js:2)
    at Function.ready (jquery-2.2.4.min.js:2)
    at HTMLDocument.J (jquery-2.2.4.min.js:2)

If I remove it, delete links for rows are not generated, even though I have in my template

<td>{% if form.instance.id %}{{ form.DELETE }}{% endif %}</td> 

So, I ended with half-solution, I can add rows, i can fill them, select2 finally works as intended, but I cannot delete rows, not when I create new items and not when I delete old ones.

The other issue is that if django creates three rows in detail table (by design) and I fill just first one it saves all three rows. Not sure how to delete rows while saving formset (tried to do form.delete while doing POST, but it doesn't work).

I can of course add more code if it would help.


Solution

  • Assuming most current jquery.formset.js from github, it needs a fix 'deleteButtonHTML' to 'delButtonHTML' that is so far not in the regular repo https://github.com/Dowsley/django-dynamic-formset/commit/1a781fd6b1a7a5f6348b43097983773f0d2a69de