3

In my application we are using a base model to hold general properties, which is a simple subclass of the ndb.Model. However, for some of the functionality it is very helpful to be able to leverage polymodel functionality. Both Model and PolyModel supposedly support multiple inheritance, my question is, are there any caveats of doing the below?

from google.appengine.ext import ndb
from google.appengine.ext.ndb import polymodel    

class InternalBase(ndb.Model):
   # some fields, methods shared to many sub-models

class Widget(polymodel.PolyModel, InternalBase):
   # widget-general

class TextWidget(Widget):
   # widget-specific

class HTMLWidget(Widget):
   # widget-specific

The goal is to have all the basic properties and methods from InternalBase available to the Widget class but also to be able to search for all subclasses of Widget using a single query (which is what the PolyModel adds).

Also, could you think of a test-case that could expose any potential problem with such scheme?

Dan McGrath
  • 41,220
  • 11
  • 99
  • 130
petr
  • 2,554
  • 3
  • 20
  • 29
  • 1
    I have been using multiple inheritance with polymodels for years ,plus additional mix in classes with no problems. – Tim Hoffman Sep 04 '13 at 23:23
  • @TimHoffman Great news! It just for some reason looked too good to be true at the first glance.. – petr Sep 05 '13 at 07:50
  • The down side is if you models get large and divergent with large numbers of big properties names and lots of entities, you will start paying an overhead as property names are stored in entities. For 2K entities with 30 properties it doesn't seem to be a problem at all. – Tim Hoffman Sep 05 '13 at 08:37
  • That's what I am trying to avoid - I will only mark as PolyModel the leafs that require the functionality - the rest of the model should be standard.. – petr Sep 05 '13 at 13:04

0 Answers0