views:

3783

answers:

5

I have a django model (schedule) with the class of entity, that is the parent of activity, that is the parent of event.

class Entity(models.Model): <...>

class Activity(models.Model): <...> team_entity = models.ForeignKey(Entity) <...>

class Event(models.Model): <...> activity = models.ForeignKey(Activity) <...>

The question is how do I serialize and get bot the child object and grand children as part of the json file.

Note: be kind, this is my first time I have posted a question (in any public form). Steven.

+3  A: 

Have a look at serializing inherited models and objects from the Django documentation available at http://docs.djangoproject.com/en/dev/topics/serialization/?from=olddocs#inherited-models

That should solve your problem.

Sarat
Thanks, I was looking at that last week. I could get a list of Entities, and a list of Activities, and a list of events. but I lost the parent child relations. Any clue what I need to do?
Steven H.
umnik below is right. I did not realise you weren't using select_related() on the model queryset.
Sarat
+6  A: 

Before you do serialization, when retrieving your objects, to preserve the relationships use select_related() to get children, grandchildren, etc

see http://docs.djangoproject.com/en/dev/ref/models/querysets/

+4  A: 

It seems to me that the question the poster was asking was to end up with a result like:

For instance, starting with these models:

class Entity(models.Model): 
    name = models.CharField(...)

class Activity(models.Model):
    name = models.CharField(...)
    team_entity = models.ForeignKey(Entity)

class Event(models.Model):
    name = models.CharField(...) 
    activity = models.ForeignKey(Activity)

Result in json:

{
     "model": "Entity", 
     "name":  "Dallas Cowboys",
     "activities": [ 
                            {
                                "model": "Activity", 
                                "name": "Practice"
                            },

                            {
                                "model": "Activity", 
                                "name": "Game"
                                "events": [
                                                   {
                                                        "model": "Event",
                                                        "name": "vs Washington Redskins"
                                                    },

                                                    {
                                                        "model": "Event",
                                                        "name": "vs Green Bay Packers"
                                                    }
                                               ]
                            }
                        ]
}

Thus keeping the parent-child-grandchild (not inheritence, but one-to-many relationship traversal). If this wasn't the initial poster's intention, I apologize...but if so I would like the answer to this as well.

gsiegman
How -- specifically -- can this be accomplished?
S.Lott
Yes this is what I am looking for. I need a group of entities, each entity will have none to many activities, each activity will have none to many events. Steven
Steven H.
+1  A: 

I believe you can find your answer here: http://code.djangoproject.com/ticket/4656

This will become part of django serializers at some stage. Right now it should be able to just replace standard django serializers with this and work away.

Bartosz Ptaszynski
+1  A: 

I now use django-piston. This does the trick.

Steven H.
Peter Mortensen