'MyModel'对象不可迭代

时间:2011-08-01 23:03:29

标签: django

我得到了'YesNo' object is not iterable..apps/wadofstuff/django/serializers/base.py in serialize, line 44

def yes_add(request, id):
    if request.is_ajax:
        form = YesNoForm(request.POST)
        if form.is_valid():
            content = form.cleaned_data['content']
            user = request.user
            value = 'yes'
            item = Item.objects.get(id=id)
            yes = YesNo(value=value, user=user, item=item, content=content)
            yes.save()
            response = HttpResponse()
            response['Content-Type'] = "text/javascript"
            response.write(serializers.serialize("json", yes))
            return response

但是我使用了这样的东西,但查询集包含了我的一个模型的几个实例。有人有任何提示吗?

2 个答案:

答案 0 :(得分:13)

您可以将实例包装在列表中:

response.write(serializers.serialize("json", [yes])

答案 1 :(得分:4)

item = Item.objects.filter(id=id)

这将返回可序列化的对象。