If you dont mind using the template system I think this should produce the results you are looking for
[{% for event in events %} {% ifchanged event.date %}{ 'date' : '{{ event.date }}', 'dayEvents' : [{% endif %} { 'title' : '{{ event.title }}', 'location' : '{{ event.location }}', 'id' : '{{ event.id }}' }, {% ifchanged event.date %}]},{% endif %} {% endfor %}] Dj Gilcrease OpenRPG Developer ~~http://www.openrpg.com On Thu, Nov 6, 2008 at 2:21 AM, Alex Koshelev <[EMAIL PROTECTED]> wrote: > It's strange. I've just try this example and it is good: > > > In [1]: results = [] > > In [2]: results.append( { 'date' : '2008-11-04 00:00:00', 'dayEvents' : > ...: [ { 'title' : 'Event 1', 'location' : '1', 'id' : '1' }, { > 'title' : > ...: 'Event 2', 'location' : '1', 'id' : '2' } ] } ) > > In [3]: from django.utils.simplejson import dumps > > In [4]: dumps(results) > Out[4]: '[{"date": "2008-11-04 00:00:00", "dayEvents": [{"id": "1", > "location": "1", "title": "Event 1"}, {"id": "2", "location": "1", "title": > "Event 2"}]}]' > > > > On Thu, Nov 6, 2008 at 12:13, Darthmahon <[EMAIL PROTECTED]> wrote: >> >> Hey Alex, >> >> I tried this hard coded JSON the other day: >> >> results = [] >> results.append( { 'date' : '2008-11-04 00:00:00', 'dayEvents' : >> [ { 'title' : 'Event 1', 'location' : '1', 'id' : '1' }, { 'title' : >> 'Event 2', 'location' : '1', 'id' : '2' } ] } ) >> >> But I got an error when I did this (can't remember the error exactly). >> >> I can't see why it would fail. >> >> On Nov 5, 10:32 pm, "Alex Koshelev" <[EMAIL PROTECTED]> wrote: >> > 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 -~----------~----~----~----~------~----~------~--~---