json响应python

时间:2015-01-05 15:26:48

标签: python json django-1.7

我正在使用django 1.7.1,我收到错误“不是JSON可序列化的”。这是我的代码

def Project_info(request):
        project_id=request.GET.get('project_id')
        people = Project_Management.objects.all().values()
        print people
        return returnSuccessShorcut ({'people': people })



def returnresponsejson(pass_dict, httpstatus=200):
    json_out = simplejson.dumps(pass_dict)
    return HttpResponse(json_out, status=httpstatus, content_type="application/json")


def returnSuccessShorcut(param_dict={}, httpstatus=200):
    param_dict['success'] = True
    return returnresponsejson(param_dict, httpstatus)

和控制台输出是: -

[{'abbreviation': u'IOS', 'acid': None, 'end_date': datetime.datetime(2014, 7, 1, 4, 59, 59, tzinfo=<UTC>), 'start_date': datetime.datetime(2014, 3, 21, 5, 0, tzinfo=<UTC>), 'user_story_id': None, 'project_name': u'2014 -KHL-347/Khaylo', 'modify_date': None, 'project_id': u'67375', 'user_name': None, 'id': 1L, 'isActive': None}]

但是当我在浏览器中点击api时,我得到了上述错误。 请提出解决方案。

1 个答案:

答案 0 :(得分:0)

在序列化之前,您需要将人员转换为列表,因为QuerySet.values返回的ValuesQuerySet默认情况下无法序列化。

def Project_info(request):
    project_id=request.GET.get('project_id')
    people = Project_Management.objects.all().values()
    people = list(people)
    return returnSuccessShorcut ({'people': people })