views:

297

answers:

3

Hi,

I have a Django model created for Google's App Engine,

Model A():
  propA = ReferenceProperty(B)

Model B():
  propB = ReferenceProperty(C)

Model C():
  propC = ReferenceProperty(B)

I have written custom Django serializer which will fetch the data for the ReferenceProperty(s) and serialize that along the initial model.

The problem occurs when I try to serialize an instance of Model A. My custom serializer will try to get propA, which contains a reference to Model C so the serializer will fetch Model C, which contains a reference to Model B and the recursion goes on and on. Is there any way to stop the recursion after a depth of say 2??

My serializer is a customized version of link text

P.S: I am willing to publish my code if that seems to needed. I have not currently attached the code since I am not at my development machine.

Thanks,
Arun Shanker Prasad.

A: 

Why don't you just do recursion properly? Any recursive operation must have a base case, otherwise it will continue forever, as your problem indicates.

dalbaeb
A: 

Just modify your functions to take a 'depth' argument. Any time you follow a ReferenceProperty, call the function with depth one less than the depth that was passed in. If a function is called with depth==0, return None, or whatever other placeholder value is suitable in your case.

Nick Johnson
A: 

I'm trying to find a serializer that works with Google App Engine and follows relationships. Would it be possible for you to post the modified code you used to do this?