Why don't you create a new list l = []
put the pagenumber etc. only a map and append to list Then iterate over your objects and append them too. Then use simplejson.dumps() and job done ? Works for me! Cheers! Sumit On Oct 20, 5:48 am, "H. de Vries" <[EMAIL PROTECTED]> wrote: > Hey Rajesh, > > Thank you so much for your comment. Somehow Google didn't sent me a > message that I had a reply on my post, so that's why I'm late with my > reply. > > I think your solution looks neat, but this way I would still need 2 > ajax requests wouldn't I? > > Doesn't self.options.pop('fields', None) delete the fields key from > the json data? > > I'm not that familiar with Python yet, so I'm probably wrong. Please > enlighten me :-) > > The dirty hack I came up with: > > data = json_serializer.serialize(users.object_list, > ensure_ascii=False, fields=('username', 'first_name', 'last_name', > 'email')) > return HttpResponse('[{"numpages": "' + > str(users.paginator.num_pages) + '", "start_index": "'+ > str(users.start_index()) +'", "end_index": "'+ str(users.end_index()) > +'", "count": "'+ str(p.count) +'", "has_next": "'+ > str(int(users.has_next())) +'", "has_previous": "'+ > str(int(users.has_previous())) +'", ' + data[2:], > mimetype='application/javascript') > > It's isn't pretty, but it works. I'm going to try your solution, I > will reply if it works. > > Greets, > > Henk. > > On 2 okt, 21:41, Rajesh Dhawan <[EMAIL PROTECTED]> wrote: > > > 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 aJSONstring 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 thejsonresponse. > > > > 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 thePaginatorhas 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- > > inJSONserializer. > > > You serializer would be something like this: > > > from django.utils import simplejson > > from django.core.serializers importjson > > > 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 django-users@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---