1

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.

6
  • can you put your models details here, your scope? Commented Feb 21, 2017 at 6:06
  • added my model class @SnakeFcz Commented Feb 21, 2017 at 6:15
  • In which colunm are you storing ['First Form Field', 'Second input Field']? Commented Feb 21, 2017 at 6:20
  • are you storing ['First Form Field', 'Second input Field'] in charfiled ? Commented Feb 21, 2017 at 6:32
  • in text_field @PiyushS.Wanare Commented Feb 21, 2017 at 6:35

2 Answers 2

2

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 %}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

3 Comments

This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.
and i am using mysql database
does't help. it stores the null value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.