0

Say I have a Django application named app. Within it I have a model class defined in models.py named data. Also, I have a html file in the template directory that has several picklists, which should be populated with the fields of the model, such as type, date. What I need to do is to get the lists of the fields in the views.py and populate the picklists with them. Another thing is that, after the user submit their query by choosing a value in the picklist, I will show them a table displaying the values of all the fields in the model. So basically I have to retrieve what the user inputs, and query by the input, and get the data of all the fields in the model based on the query. We could name another two fields time, value. These things should be mostly done in views.py.

Here are the picklists in the hmtl file:

 <form action="" method="post">
 <select name="type" values="type_data"></select>
 <select name="date" values="date_data"></select>
 <input type="submit" value="Submit">
 </form>

What I tried, for the two things, are:

1.To get the data to populate the picklist, I used:

objects= data.objects.all()
type_data=getattr(objects, 'type')
date_data=getattr(objects, 'date')

2.To get the data after the query, I used:

if request.method == 'POST':
     …
    mytype =  '% r' % request.POST.get['type']
   mydate = ' %r ' % request.POST.get['date']
   objects2= data.objects2.filter(type=mytype,date=mydate)
   time=getattr(objects2, 'time')
   value=getattr(objects2, 'value')

It does not work. I am referencing this link django object get/set field so I’m basically using this to get field data from model: getattr(obj, 'field_name')

Any thoughts? I appreciate any help

Community
  • 1
  • 1
widget
  • 945
  • 3
  • 13
  • 22

2 Answers2

1

Have a look at Django's Form functionality – specifically ModelChoiceField – it solves this problem.

If you want to introspect a model to see what fields it has, have a look at model._meta.fields.

For example, the following code prints out the value for each field on a model instance instance:

for field in instance._meta.fields:
    print field.attname, '=', getattr(model, field.attname)
bradley.ayers
  • 37,165
  • 14
  • 93
  • 99
  • i tried these codes but it didnot work. it does not seem that it recognize this _meta.fields thing – widget Apr 08 '11 at 01:52
1

your objects variable is a QuerySet, when materialized is a "list of objects", so to get list of object properties you should iterate over them:

objects = data.objects.all()

type_data = []
date_date = []

for obj in objects:
    type_data.append(obj.type)
    date_date.append(obj.date)

but i suppose what you will really need for the dropdown is to have an id for each value (but i may be wrong):

for obj in objects:
    type_data.append((obj.pk, obj.type))
    date_date.append((obj.pk, obj.date))

this will create a list of tuples:

[(1, 'value1'), (2, 'value2')...]

so in the template you can use:

<select name="type">
    {% for value, name in type_data %}
        <option value="{{ value }}">{{ name }}</option>
    {% endfor %}
</select>

but have in mind, that django can simplify this process - read about forms.

Jerzyk
  • 3,662
  • 23
  • 40
  • u r right that i should iterate over them to get the object properties. About the id, I actually don't need it for now, i just need to show the value the user pick and query by it. now I'm getting an error: 'instancemethod' object is unsubscriptable. I'm thinking that if this is the Right way to access objects in model. to simply use: objects = data.objects.all() Is it really getting the objects for me?? – widget Apr 07 '11 at 15:48
  • i made a mistake in the table name in my database. it should be appl_class, while i only had class as the name. sb pointed that out for me. – widget Apr 08 '11 at 01:51
  • It turnes out that I could simply use: 'list=objects.values_list(fieldname, flat=True)' to get the list of the field values. See this documentation http://docs.djangoproject.com/en/dev/ref/models/querysets/ – widget Apr 14 '11 at 02:22
  • but doing this twice will generate two separate hist to the DB – Jerzyk Apr 14 '11 at 10:59