SQL cant do this, so do it in python...

items = ItemOwned.objects.filter(user=id)
data = { }
for item in items:
  cur_set = data.get(item.setId, {'setId': item.setId, 'items':[]})
  cur_set['items'].append({'id': item.id, })
  data[item.setId] = cur_set
return HttpResponse(content=simplejson.dumps(data.values()),
mimetype='application/json')


On Fri, Oct 30, 2009 at 10:52 AM, The Danny Bos <danny...@gmail.com> wrote:

>
> Any ideas anyone?
> I'm completely stumped ...
>
>
>
>
> On Oct 30, 12:59 pm, The Danny Bos <danny...@gmail.com> wrote:
> > If helpful, here's the table structure.
> >
> > class ItemOwned(models.Model):
> >         user = models.ForeignKey(User)
> >         item = models.ForeignKey(Item)
> >
> > class Item(models.Model):
> >         set = models.ForeignKey(Set)
> >         number = models.IntegerField()
> >
> > class Set(models.Model):
> >         title = models.CharField(max_length=50, unique=True)
> >
> > On Oct 30, 11:52 am, The Danny Bos <danny...@gmail.com> wrote:
> >
> >
> >
> > > Hey there, I've got the below query, generating the below JSON. But I
> > > want to group them by "setId", can I do this in my query or in
> > > views.py to create a JSON file like the one at the bottom? Regroup in
> > > a template would be great, but doesn't work in views ...
> >
> > > views.py:
> > > items = ItemOwned.objects.filter(user=id)      ### Obviously simple at
> > > the moment.
> >
> > > generates JSON:
> > > {"setId": 1, "id": 75},
> > > {"setId": 1, "id": 76},
> > > {"setId": 1, "id": 77},
> > > {"setId": 2, "id": 78},
> > > {"setId": 2, "id": 79},
> >
> > > what I need:
> > > {"setId": 1, items: [{ "id": 75},{ "id": 76},{ "id": 77}] },
> > > {"setId": 2, items: [{ "id": 78},{ "id": 79}] },
> >
> > > Know what I mean?
> > > Seems like a grouping issue to me.
> >
> > > I could do a few sub-queries but the results are quite large so it'd
> > > be best to do it well.
> > > Any code examples would be great as I'm a beginner trying his best ...
> >
> > > Thanks,
> >
>

--~--~---------~--~----~------------~-------~--~----~
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 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to