Django Json通过id过滤了查询集

时间:2018-04-16 16:12:35

标签: json django django-queryset

我的目标是获取id并使用id过滤查询集,如下面的代码所示:

views.py

class MyProfile(TemplateView):
model = Reports
template_name = 'template.html'
def get_context_data(request, *args, **kwargs):
    if kwargs.get('pk', None):
        q = kwargs.get('pk', None)
        queryset = Reports.objects.all().values('id','line_x','line_y',).filter(id = q)
        data = list(queryset)
        return JsonResponse(data, safe=False)

urls.py

url(r'^profiles/(?P<pk>\d+)/$', views.MyProfile.as_view())

它返回以下错误:

上下文必须是dict而不是JsonResponse

Django 1.11.8

1 个答案:

答案 0 :(得分:1)

from django.http import JsonResponse

def different_function_name(request, *args, **kwargs):
    if kwargs.get('pk', None):
       q = kwargs.get('pk', None)
       queryset = Reports.objects.all().values('id','line_x','line_y',).filter(id = q) 
       query_list = list(queryset)
       return JsonResponse(query_list, safe=False)