开发者

How to get field data from model?

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


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜