krzyhub
krzyhub

Reputation: 6540

How can I append additional dictionary to my model_list?

I get 'Pixel' object does not support item assignment

How can I append additional dictionary to my pixel_list objects?

def pixel_main(request, page):
    y = int(page) * 10
    x = y - 10
    pixel_list = Pixel.objects.all()[x:y]
    for i in pixel_list:
        if Handler.objects.filter(pixel=i.id).filter(user=request.user):
            i['vote'] = True
        else:
            i['vote'] = False

    return render_to_response('pixel_main.html', {"pixels": pixel_list}, context_instance=RequestContext(request))

Upvotes: 0

Views: 99

Answers (2)

Michał Modzelewski
Michał Modzelewski

Reputation: 1320

Python lets you add attributes to object instances using dot notation:

for i in pixel_list:
    if Handler.objects.filter(pixel=i.id).filter(user=request.user):
        i.vote = True
    else:
        i.vote = False

Upvotes: 2

Fred Foo
Fred Foo

Reputation: 363757

You could add a BooleanField to the pixel class in your models.py, if the vote should be saved.

Alternatively, you can pass the votes to the view in a separate object:

vote = defaultdict(bool)
if Handler.objects.filter(pixel=i.id).filter(user=request.user):
    vote[i] = True

Upvotes: 1

Related Questions