How to dumps list of object in to json in python django

1k views Asked by At

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

here is my code 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

    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

There are 1 answers

2
math2001 On BEST ANSWER

99% the problem is coming from

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?