lab user lab user - 1 year ago 81
Python Question

Understanding classes in ndb model - GAE

I have file which has a Class Parent(ndb.model) with different properties. What would happen if I pass Parent as the parameter to another class, such that

class child(Parent)

Can I define the properties of Parent in Class child, if I am Hard Coding ?

Answer Source

Yes, that will work, and child classes will inherit parent properties:

class Parent(ndb.Model):
    lastname = ndb.StringProperty()

class Child(Parent):
    name = ndb.StringProperty()

Child(lastname='Doe', name='John').put()

You could have as many mix-ins and childs as you want, properties will be resolved correctly following language [python] rules.

Just keep in mind that only the actual saved entity will be in the datastore, Child in this example. In other words, only this kind will exist.

If you would like to query by the parent, to get all Animals for a canonical example, you would need to use a PolyModel.