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