Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying and saving Django ArrayField

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?

like image 293
Alexis Avatar asked Oct 29 '22 04:10

Alexis


1 Answers

I do not know what is your exact goal but it can be something like this:

{% for element in my_list %}
    <div> {{element}} </div> <!--any html tag which you desire--> 
{% endfor %}
like image 71
pooya Avatar answered Nov 09 '22 06:11

pooya