如何将附加字典附加到我的model_list?

时间:2011-07-05 20:16:32

标签: python django

我得到'Pixel' object does not support item assignment

如何将附加字典附加到pixel_list对象?

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))

2 个答案:

答案 0 :(得分:2)

Python允许您使用点表示法向对象实例添加属性:

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

答案 1 :(得分:1)

如果应保存models.py,您可以在vote的像素类中添加BooleanField

或者,您可以在单独的对象中将投票传递给视图:

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