Circular references in db.Model definitions ?

22 views
Skip to first unread message

Sylvain ZIMMER

unread,
Apr 8, 2008, 6:27:22 PM4/8/08
to Google App Engine
Hi,

I've got a simple problem and I'm sure there is a simple solution but
I haven't found it yet.

I try to do the following :


from google.appengine.ext import db

class Player(db.Model):
currentgame=db.ReferenceProperty(Game)


class Game(db.Model):
player1=db.ReferenceProperty(Player,collection_name="player1_set")
player2=db.ReferenceProperty(Player,collection_name="player2_set")


So of course, "Game" isn't available when first going through the
Player definition.

I tried doing "Player.currentgame=db.ReferenceProperty(Game)" after
the Game definition, which may or may not be the right way, but then I
get this error when setting the currentgame property :

ERROR 2008-04-08 22:19:18,330 __init__.py] Traceback (most recent
call last):
File "/usr/local/google_appengine/google/appengine/ext/webapp/
__init__.py", line 486, in __call__
handler.post(*groups)
File "/Users/sylvainzimmer/w/svnjamendo/trunk/googlemusicgame/
ajax.py", line 29, in post
resp = self.method(method)
File "/Users/sylvainzimmer/w/svnjamendo/trunk/googlemusicgame/
ajax.py", line 92, in method
found.currentgame=game.key()
File "/usr/local/google_appengine/google/appengine/ext/db/
__init__.py", line 2234, in __set__
setattr(model_instance, self.__id_attr_name(), value)
File "/usr/local/google_appengine/google/appengine/ext/db/
__init__.py", line 2280, in __id_attr_name
return self._attr_name()
File "/usr/local/google_appengine/google/appengine/ext/db/
__init__.py", line 468, in _attr_name
return '_' + self.name
TypeError: cannot concatenate 'str' and 'NoneType' objects


So that's it, thanks for any help!

Rafe

unread,
Apr 8, 2008, 9:23:29 PM4/8/08
to Google App Engine
No support circular references yet, however I can provide you with a
that is not great but will get you through.

def add_property(cls, name, property):
setattr(cls, name, property)
getattr(cls, name).__property_config__(Player, 'currentgame')
cls._properties[name] = property

class Player(db.Model):
pass

class Game(db.Model):
player1 = db.ReferenceProperty(Player,
collection_name='player1_set')
player2 = db.ReferenceProperty(Player,
collection_name='player2_set')

add_property(Player, 'currentgame', db.ReferenceProperty(Game))

game = Game()
game.put()

player1 = Player(currentgame=game)
player2 = Player(currentgame=game)
player1.put()
player2.put()

game.player1 = player1
game.player2 = player2
game.put()

Brett Morgan

unread,
Apr 8, 2008, 9:28:27 PM4/8/08
to google-a...@googlegroups.com
Stupid question of the day. What is the difference between circular
references and self references?

http://code.google.com/appengine/docs/datastore/typesandpropertyclasses.html#SelfReferenceProperty

Sylvain ZIMMER

unread,
Apr 8, 2008, 9:41:40 PM4/8/08
to Google App Engine
Rafe:

Thank you for add_property. I hope something like that will be
included in the API.

Brett:

I think self reference is when you'd do:

class Game(db.Model):
nextgame = db.SelfReferenceProperty()

On Apr 9, 11:28 am, "Brett Morgan" <brett.mor...@gmail.com> wrote:
> Stupid question of the day. What is the difference between circular
> references and self references?
>
> http://code.google.com/appengine/docs/datastore/typesandpropertyclass...

Rapheal Kaplan

unread,
Apr 8, 2008, 9:43:16 PM4/8/08
to google-a...@googlegroups.com
  That's not a dumb question.  A self reference is useful for modeling hierarchies, like the nodes of an XML document.  Circular references are necessary to implement many-to-many relationships.

  Self reference is much easier using db.Model:

  class XMLNode(db.Model):
    parent = db.SelfReferenceProperty()
--

- 'Chops

Brett Morgan

unread,
Apr 8, 2008, 9:44:00 PM4/8/08
to google-a...@googlegroups.com
Sylvain,

Your explanation contradicts with my understanding of the docs:

"A reference to another model instance of the same class."

Rapheal Kaplan

unread,
Apr 8, 2008, 9:46:15 PM4/8/08
to google-a...@googlegroups.com
  The docs say that it's a reference to a "model instance".  In the XMLNode example, XMLNode is the Model whereas XMLNode() creates a new model instance.
--

- 'Chops
Reply all
Reply to author
Forward
0 new messages