Hi,

> I have the following:
> [snip]
> def people_json(request):
>         userobjects = User.objects.filter(is_superuser=False).extra(order_by
> = ['auth_user.username+0'])
>         p = Paginator(userobjects, 10)
>         pagenumber = request.POST.get('pagenumber', 1)
>         try:
>                 users = p.page(pagenumber)
>         except:
>                 users = p.page(1)
>         json_serializer = serializers.get_serializer("json")()
>         data = json_serializer.serialize(users.object_list,
> ensure_ascii=False, fields=('username', 'first_name', 'last_name',
> 'email'))
>         return HttpResponse(data, mimetype='application/javascript')
> [/snip]
>
> This will return a JSON string to the browser that I use to make a
> people overview that uses AJAX.
> Anyway, because I have to draw 'previous' and 'next' buttons, I want
> to attach some extra data to the json response.
>
> For example I would like to concatenate the following list to the
> response:
> pageinfo = [p.start_index(), p.end_index(), p.paginator.count,
> p.has_next(), p.has_previous()]
>
> I tried to convert users.object_list to a list and concatenate it with
> pageinfo, but that doesn't work ( tells me str doesn't have the
> attribute _meta ).
> I don't want to use html as a response because that would throw more
> bytes over the pipe. ( it's also slower ).
> I also don't want to use 2 separate requests because that would be
> more overhead ( 2 separate requests take more time and it would also
> result in to 2 queries because the Paginator has to be initiated once
> again ).
>
> Does anyone have a good solution for this problem? Using Django has
> been a great experience but things like this take a looooot of time.

You could register your own[1] serializer class that extends the built-
in JSON serializer.

You serializer would be something like this:

from django.utils import simplejson
from django.core.serializers import json

class Serializer(json.Serializer):
    def __init__(self, dict):
        self.dict = dict or {}
    def end_serialization(self):
        self.options.pop('stream', None)
        self.options.pop('fields', None)
        self.dict['objects'] = self.objects
        simplejson.dump(self.dict, self.stream,
cls=json.DjangoJSONEncoder, **self.options)


See this setting to register your own serializer:
[1] http://docs.djangoproject.com/en/dev/ref/settings/#serialization-modules

Let's say you registered this custom serializer as "my_json". You'd
use it like this:

dict = {'pageinfo':pageinfo} # where pageinfo is your previously
mentioned list
json_serializer = serializers.get_serializer("json")(dict)
data = json_serializer.serialize(users.object_list,
ensure_ascii=False, fields=('username', 'first_name', 'last_name',
'email'))

The "dict" can contain any keys and values that
django.utils.simplejson can encode.

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

Reply via email to