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