Sidharth
Sidharth

Reputation: 1462

How to get List from Database to html in python - Django?

i have few items/List stored in database stored as ['First Form Field', 'Second input Field']

i want all these items of a column as a list in my front end html page I am getting items as

ff= form_fields.objects.all() return render(request,'user/index.html',{'message': welcome,'ff': ff})

INDEX:

 {% for x in ff%}
        <tr>
            <td>
                {{ x.id}}
            </td>
            <td>
                {{ x.name }}
            </td>
            <td>
                {% for y in x.text_field %}
              <label>{{ y }}</label>

                    {% endfor %}
            </td>
            <td>

models

class form_fields(models.Model):
  id = models.AutoField(primary_key=True)
  form_name = models.CharField(max_length= 50, null=True)
  text_field = models.CharField(max_length=255, null= True)

I want that saved list to be in that table as list.

Upvotes: 1

Views: 1698

Answers (2)

Cadmus
Cadmus

Reputation: 665

create own ListField field for resolving this issue

from django.db import models
import ast

class ListField(models.TextField):
    __metaclass__ = models.SubfieldBase
    description = "form fields"

    def __init__(self, *args, **kwargs):
        super(ListField, self).__init__(*args, **kwargs)

    def to_python(self, value):
        if not value:
            value = []

        if isinstance(value, list):
            return value

        return ast.literal_eval(value)

    def get_prep_value(self, value):
        if value is None:
            return value

        return unicode(value)

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return self.get_db_prep_value(value)

class Form_Fields(models.Model):
    id = models.AutoField(primary_key=True) ## id builtin in django models
    form_name = models.CharField(max_length= 50, null=True)
    form_fields = ListField()


[1]:Form_Fields.objects.create(form_name="test", form_field=['First Form Field', 'Second input Field'])


{% for x in ff%}
        <tr>
            <td>
                {{ x.id}}
            </td>
            <td>
                {{ x.form_name }}
            </td>
            <td>
                {% for y in x.form_fields %}
                    <label>{{ y }}</label>
                {% endfor %}
            </td>
            <td>
     </tr>
{% endfor %}

Upvotes: 2

Piyush S. Wanare
Piyush S. Wanare

Reputation: 4933

Change models.py to this:-

from django_mysql.models import JSONField, Model

class form_fields(models.Model):
  id = models.AutoField(primary_key=True)
  form_name = JSONField()
  text_field = models.CharField(max_length=255, null= True)

views.py:-

form_fields.objects.create(id=id, attrs=['First Form Field', 'Second input Field'],text_field='TextField')

form_fields.objects.filter(attrs=['First Form Field', 'Second input Field'])

You are adding array in character fields so you will getting each thing as a character.Other things whatever you have code will work fine.

Upvotes: 1

Related Questions