Displaying and saving Django ArrayField

Viewed 2285

I have an a type of ArrayField in a model. The backend in a list of suggested elements for my_list automatically. However, the user then needs to update this list as needed. The model looks like this:

class my_model(models.Model):
    my_list = ArrayField(
        models.CharField(max_length=10)
    )

The problem is that when my_list is rendered in the template is a single html input tag with a comma separated list. For example a,b,c,d.

My question is two fold. How can I get the templating language to display the comma separated list so that each element has their own html input box. Moreover, the harder thing that I am struggling with is how to make it so that these elements will be saved back to the model back as an array.

My current thinking is to hack the front-end with javascript. But is there a better way to do it with Django?

3 Answers

You can create several fields with name attribute 'myfield' for example. Then in the View you can get a list of that fields using request.POST.getlist('myfield') and you can iterate it in a loop for and store it in an arrayfield. 2 Years later but i hope this solve your question.

Related