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 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
精彩评论