Tuesday, September 28, 2010

How to get an ajax call to return both QuerySet and paging info in Django

I am trying to implement paging across ajax calls. The page should not
refresh when the user wants to see the next x num of results.

Here is my problem. Returning the QuerySet is super simple. I just do
(sumaJson is custom)

data = serializers.serialize('sumaJson', result_page.object_list,
relations=('first_major', 'country_of_origin', 'second_major'))
return HttpResponse(data, mimetype="application/json")

Now I also want to return things like

result_page.has_previous()
result_page.has_next()
result_page.paginator.count

and so on. I for the life of me can't figure out how to get both
across in one response. I can't add this info to
result_page.object_list because then the serializer fails. If I
something of the sort of

simplejson.dumps(paging_info + result_page.object_list)

Then in the javascript the QuerySet is no longer a list of objects but
just a big string of characters which can't be interpreted with

$.each(data.data, function(index, item){

I tried some bad hacks like creating a fake object and putting it in
the object_list, serializing this and then deleting the object. This
allows me to get the data across. However, I don't want to be creating
and deleting fake objects.

I don't want to meddle with the serializer. I don't want to send a
second ajax request once I get the querySet back to get the paging
info.

Am I missing something? Is there an easy way to get both across in one
response? Thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment