0

I am working on a simple online database for sharing technical equipment between artists.

I would like to let a class User() own entities of other classes such as a Lamp() or a Speaker().

It seems that I could be using ReferenceProperty when using db, but when I look through the documentation for ndb it seems a bit confusing to me, as I can't find ReferenceProperty.

So If, using ndb, I want to let an entity of the class Lamp() be owned by an entity of the class User(), by referencing the id of a User(), what should I do? Wanting it to be something like what I have written below, although I am pretty confident that what I have written below does not in fact work:

class User(ndb.Model):
    name = ndb.StringProperty()
    email = ndb.....

class Lamp(db.Model):
    owner = db.ReferenceProperty(User)
Barry Michael Doyle
  • 9,333
  • 30
  • 83
  • 143

1 Answers1

0

What you're looking for is ndb.KeyProperty(kind="User") as shown in https://cloud.google.com/appengine/docs/python/ndb/entity-property-reference

That way if you have a user u for example and want all the lamps owned by u you could get them this way:

lamps = Lamp.query(Lamp.owner == u.key).fetch()

And if you have a lamp l and want to get the owner of l you could get it this way:

owner = l.owner.get()

You could also use ancestors to reference your clases without specifying the KeyProperty as shown in https://cloud.google.com/appengine/docs/python/ndb/queries

Jorge Caballero
  • 611
  • 9
  • 11