如何在python django中将对象列表转储到json中

时间:2022-10-30 19:21:56

Here i'm trying to pass json to javascript. But it returns empty list. i need to dump my list of data in to json format.but the json is empty.please help me

在这里,我试图将json传递给javascript。但它返回空列表。我需要将我的数据列表转储为json格式。但是json是空的。请帮助我

here is my code views.py

这是我的代码views.py

from django.utils import simplejson

def student_search(request):
   location = rg('location')
   results = StudentProfile.objects.filter(Q(address_line1__icontains=location) \
                                                        | Q(address_line2__icontains=location)).values()
   for result in results:
       students_list.append(result)
   if request.is_ajax():
       result = {

            'student': [student._json() for student in students_list]
            }
       return HttpResponse(
            simplejson.dumps(result), content_type="application/json",
            )
   return render_to_response("account/students.html",{'students_list' : students_list }, context_instance= RequestContext(request))

urls.py

urls.py

    url(r'student_search', 'account.views.student_search', name='student_searchview'),

template

模板

    <div class="hold"></div>
<input type="button" id="btnLoadMore" value="Load More"/>
<script type="text/javascript">
    $(document).ready(function () {
        $.get("/student_search/", function (data) {
            alert ("enter");
            alert(JSON.stringify(data));

        });

    });
    </script>

1 个解决方案

#1


0  

99% the problem is coming from

99%的问题来自

results = StudentProfile.objects.filter(Q(address_line1__icontains=location) \
                                                    | Q(address_line2__icontains=location)).values()

Are you absolutely sure this returns a list of bool, float, int, string, list, or dict?

你绝对确定这会返回bool,float,int,string,list或dict的列表吗?

#1


0  

99% the problem is coming from

99%的问题来自

results = StudentProfile.objects.filter(Q(address_line1__icontains=location) \
                                                    | Q(address_line2__icontains=location)).values()

Are you absolutely sure this returns a list of bool, float, int, string, list, or dict?

你绝对确定这会返回bool,float,int,string,list或dict的列表吗?