0
votes

I have a situation in which a Model that I use has a number of properties needed for all entities and a few that are specific to one of two alternatives, like in the following example:

class Machine(ndb.Model):

    #Properties for all machines:
    price = ndb.FloatProperty()
    model = ndb.StringProperty()
    vendor = ndb.KeyProperty()
    #...

    #Properties exclusive to type A machines:
    foo1 = ndb.StringProperty()
    foo2 = ndb.StringProperty()

    #Properties exclusive to type B machines:
    bar1 = ndb.StringProperty()
    bar2 = ndb.StringProperty()

From what I understand, I could take one of two paths. Either I make it a regular ndb.Model and just leave some properties without content or I could use an ndb.Expando Model and set the fixed properties in advance and just add the others depending on the case.

As I understand it, the Expando Model is useful when you don't know the properties that you will need, but I do know in advance all the properties needed (foo1, foo2, bar1, bar2). Should I use Expando or is it alright to use a regular ndb.Model and leave a few properties blank for each entity?

1

1 Answers

1
votes

For your scenario I would seriously consider using ndb.PolyModel

class Machine(ndb.Model):

    #Properties for all machines:
    price = ndb.FloatProperty()
    model = ndb.StringProperty()
    vendor = ndb.KeyProperty()

class MachineA(Machine):
    foo1 = ndb.StringProperty()
    foo2 = ndb.StringProperty()

class MachineB(Machine):
    bar1 = ndb.StringProperty()
    bar2 = ndb.StringProperty()

This then allows you to query for all Machine's,

by Machine.query() or individual Machine types with MachineB.query()

Just my 2c. (I have a number of projects using this. For instance, a Product and different Product types that have different additional properties), e.g a Plant vs a Pump, both being basic Products, with price, categorisation, photos, etc) but a plant will have different classifications (flowering, non-flowering) and a pump will have flow rate, watts, etc) But I can find all all products less < $25 (plant or otherwise) by Product.query(Product.price < 25)