babbaggeii
babbaggeii

Reputation: 7737

django queryset manipulation

I have a queryset within a view and want to manipulate/edit before returning it to the template.

But if I want to convert a queryset to values (below) - so that I can manipulate it - I can't then pass it onto the template. Do I have to convert it back to a queryset? How would I do that?

x = RandomModel.objects.values_list()

doesn't work, but

x = RandomModel.objects.all()

does.

I'm manipulating it first, with (e.g.):

x[3][4]="test"

But even without the manipulation, I can't access it from the template.

I'm passing it to the template with:

return render_to_response('index.html', {
    'design_list': x,    
})

In the template, I'm trying to access the data with (e.g.):

{% for item in design_list %}
    {{ item.title }}
{% endfor %}

Upvotes: 2

Views: 1219

Answers (1)

Serhii Holinei
Serhii Holinei

Reputation: 5864

Just as I said in a comment before, you neglect tuple's index. Try next:

{% for item in design_list %}
    {{ item.1 }} {# item is a tuple, not a dict #}
{% endfor %}

Note the index I use in line {{ item.1 }}, it returns second element of a tuple (first is usually id). So if x = [(1,'title1'), (2,'title2'), ...], you will see title1 and title2 on a template.

Upvotes: 2

Related Questions