views:

50

answers:

2

How can I get access to the implicit property names of a db.Model in Google App Engine? In particular, assume I have the following:

class Foo(db.Model):
  specific = db.IntegerProperty()


class Bar(db.Model):
 foo = db.ReferenceProperty(Foo, collection_name = "bars") 

if I attempt to get the property names on Foo, like so:

my_foo = Foo(specific = 42)
for key, prop in my_foo.properties().iteritems()
    print "HERE bars won't show up"

then the my_foo.bars property doesn't show up. Or am I totally mistaken?

Any help greatly appreciated

edited model to be Python, not ruby

+2  A: 

(That model definition looks like a strange hybrid of Python and Ruby.)

I'm not clear on what you're trying to achieve here, but you can get a list of model property members using introspection:

[x for x in dir(Foo) if isinstance(getattr(Foo,x), db.Property)]

If you're just trying to add instances of Bar to Foo, you should create new Bar instances with their foo fields pointing at the Foo instance:

foo = Foo(specific=42)
foo.put()
Bar(foo=foo).put()
Bar(foo=foo).put()
logging.info("Foo bars: %r" % list(foo.bars))
greensnark
ReferenceProperties do not permanently store a back-references on the class. So an attribute with the name specified in collection_name is not created unit `Bar` has been defined.
Robert Kluin
Fixed the model defs.
Einar Vollset
Your suggestion on getting the properties doesn't seem to work (though that is what I'm trying to do). Guess I'll just use ListProperties of keys..
Einar Vollset