Re: Is it possible to output JSON like this?

2008-11-07 Thread Dj Gilcrease

On Fri, Nov 7, 2008 at 12:17 AM, Darthmahon <[EMAIL PROTECTED]> wrote:
>
> Hi guys, I'll give both a try tomorrow when I am near my dev box
> again. Can't really do it in the template as I just want to dump out
> JSON only, but I'll give them both a go :)

I use templates for all my JSON output, make it easier for me to
change it if/when I need

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



Re: Is it possible to output JSON like this?

2008-11-06 Thread Darthmahon

Hi guys, I'll give both a try tomorrow when I am near my dev box
again. Can't really do it in the template as I just want to dump out
JSON only, but I'll give them both a go :)

Cheers,
Chris

On Nov 6, 9:51 am, "Dj Gilcrease" <[EMAIL PROTECTED]> wrote:
> 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 codedJSONthe 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
> >> > toJSONany 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 intoJSON:
>
> >> > > from django.core import serializers
> >> > >json= serializers.serialize("json", Event.objects.all()[:5],
> >> > > fields=('title','date','location'))
>
> >> > > Now, this returnsJSONlikethis:
>
> >> > > [
> >> > >        { "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 outputlikethis:
>
> >> > > [
> >> > >        { '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 lookslikethis:
>
> >> > > 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
-~--~~~~--~~--~--~---



Re: Is it possible to output JSON like this?

2008-11-06 Thread Dj Gilcrease

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



Re: Is it possible to output JSON like this?

2008-11-06 Thread Alex Koshelev
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
-~--~~~~--~~--~--~---



Re: Is it possible to output JSON like this?

2008-11-06 Thread Darthmahon

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



Re: Is it possible to output JSON like this?

2008-11-05 Thread Alex Koshelev
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
-~--~~~~--~~--~--~---



Is it possible to output JSON like this?

2008-11-05 Thread Darthmahon

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