Django没有在JsonResponse中返回正确的对象数据

时间:2015-08-21 08:12:28

标签: json django

我正在尝试返回JSON数据,但它返回字段,模型和pk。 这是我的代码:

project.py

def data_list_projects(request):

    user_id = request.session['user_id']
    if not user_id:
        return HttpResponse(JsonResponse({'message': 'not logged'}), content_type="application/json")

    projects = Project.objects.filter(status='active')

    if projects and len(projects) > 0:
        response_data = {'found': True, 'projects': serializers.serialize('json', projects), 'message': 'logged'}
    else:
        response_data = {'found': False, 'message': 'logged'}

    return JsonResponse(response_data, content_type="application/json")

list.js

$(function(){

    getProjects();
});

function getProjects(){

    $.ajax({
        url: root + 'data-list-projects',
        type: 'get',
        dataType: 'json',
        success: function(result){
             console.log(result);
        }
    });
}

当我在Google Chrome上查看回复时,我得到:

{"found": true, "message": "logged", "projects": "[{"fields": {"status": "active", "created_at": "2015-08-20T10:08:46Z", "name": "a", "created_by": 1, "description": "b"}, "model": "app.project", "pk": 1}]"}

这在Laravel和Rails中恢复正常。为什么没有项目对象的数据。我需要JSON响应是这样的:

{"found": true, "projects":[{"id": "10", "name": "Project 1", "description": "Contain the bugs from Site.", "created_by": "4", "status": "active", "created_at": "2015-06-26 12:40:50", "updated_at": "2015-08-11 11:42:40"}], "message": "logged"}

0 个答案:

没有答案