[Django]-'unicode' object has no attribute 'value1'

3👍

myobjects.values_list() returns a list of unicode values, not objects. If you need to augment the values with values from your queryset, you might consider returning a dictionary of values instead of the values_list…

times = myobjects.values('time')

for mytime in times:
    mytime.update({'my_key' : myobjects.filter(time=mytime).values_list('value1',flat=true)})

Hope that helps you out.

1👍

I wonder why you pass a flattened queryset (values_list) to the template at all?

As Brandon mentioned you would be better off if you put your data in a dict:

myobjects=model.objects.all()
times=myobjects.values_list(‘time’, flat=True)

time_info = {}

for mytime in times:
    time_info[mytime] = myobjects.filter(time=mytime)

And then in your template:

{% for time, myobjects in time_info.items %}
<h1>{{ time }}</h1>
<ul>
{% for myobject in myobjects %} 
    <li>{{ myobject.value }}</li> 
{% endfor %}
</ul>
{% endfor %}
👤arie

Leave a comment