JavaScript Array Widget
-
Comments:
- here.
I’ve been making more and more use of the django.contrib.postgres
classes, and will often store data in an ArrayField
where appropriate.
There are two form fields that are supplied with Django for handling these types: one of which has the array values in a single text input (comma separated), and the other has a different text input element for each value.
However, the latter does not really work that well with a dynamic length array (it could work with up to N items, but in my case, I really don’t often have an N).
It could be possible to build similar functionality that you see with the Django Admin’s formset handling like here, however this turns out to be lots of mucking around.
It might be simpler to just have the simple array field rendered, and have JS bolted on that builds the dynamic list of text inputs based on this.
In this instance, I am actually storing the state in the widgets themselves: this means it’s relatively easy to add in the ability to re-order. I’ve done this with the Sortable library.