Tuesday, September 28, 2010

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

On 9/28/2010 10:42 PM, Dmitrij wrote:
> 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!
>
I don't know about a fake object, but surely if you put everything you
want in a Python dict and then serialize that it will arrive in
JavaScript as an object with named fields, which you can use as you want
by referencing the fields as object attributes. Or am I missing something?

regards
Steve

--
DjangoCon US 2010 September 7-9 http://djangocon.us/

--
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