开发者

How can I append additional dictionary to my model_list?

开发者 https://www.devze.com 2023-03-17 06:35 出处:网络
I get \'Pixel\' object does not support item assignment How can I append additional dictionary to my pixe开发者_如何学Gol_list objects?

I get 'Pixel' object does not support item assignment

How can I append additional dictionary to my pixe开发者_如何学Gol_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))


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


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
0

精彩评论

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