With simplejson module (that is part of django distribution) you can covert to JSON any python objects. So create data structure you need and pass it to simplejson dump/dumps functions.
On Thu, Nov 6, 2008 at 01:17, Darthmahon <[EMAIL PROTECTED]> wrote: > > Hi, > > Ok I'm using the following code to turn my model into JSON: > > from django.core import serializers > json = serializers.serialize("json", Event.objects.all()[:5], > fields=('title','date','location')) > > Now, this returns JSON like this: > > [ > { "pk": 1, "model": "events.event", "fields": { > "date": "2008-11-04 00:00:00", "location": 1, "title": > "Event 1"} > }, > { "pk": 2, "model": "events.event", "fields": { > "date": "2008-11-04 00:00:00", "location": 1, "title": > "Event 2"} > }, > { "pk": 3, "model": "events.event", "fields": { > "date": "2008-11-05 00:00:00", "location": 1, "title": > "Event 3"} > }, > { "pk": 4, "model": "events.event", "fields": { > "date": "2008-11-05 00:00:00", "location": 1, "title": > "Event 4"} > }, > ] > > But I want it to output like this: > > [ > { 'date' : '2008-11-04 00:00:00', 'dayEvents' : [ > { 'title' : 'Event 1', 'location' : '1', 'id' : '1' }, > { 'title' : 'Event 2', 'location' : '1', 'id' : '2' } > ]}, > { 'date' : '2008-11-05 00:00:00', 'dayEvents' : [ > { 'title' : 'Event 3', 'location' : '1', 'id' : '3' }, > { 'title' : 'Event 4', 'location' : '1', 'id' : '4' } > ]} > ] > > The main difference is that the events are grouped by date, which > makes it easier for me to loop through them the way I want to. > > Any ideas on how to achieve this, been looking in the documentation > but nothing in there from what I can see :( > > The event model looks like this: > > title = models.CharField(max_length=200) > date = models.DateTimeField() > location = models.IntegerField(choices=LOCATION_CHOICES, default=1) > > Cheers, > Chris > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---