title: Making a smart class factory in Django
body: <p>I've been trying to figure this out for a while now with little success.  I'm attempting to write a class factory that plays nice with Django's ORM, so that I can take a model schema like this:</p>
  
  <pre><code>Product
      SubclassOfProduct0
      SubclassOfProduct1
      ....
  </code></pre>
  
  <p>To work like this:</p>
  
  <pre><code>Product.objects.get(pk=7) // returns the result of SubclassOfProduct0(pk=7)
  Product.objects.filter(propname="w00t") // returns a QuerySet of Product objects
  </code></pre>
  
  <p>So I was thinking something like this:</p>
  
  <pre><code>class ProductManager(models.Manager):
      def get(self, *a, **kwa):
          # Get the id from Products (somehow)
          if product.type == Product.TYPE_SUBCLASS0:
              return ProductSubClass0.objects.get(pk=kwa["pk"])
  
  
  class Product(models.Model):
  
      TYPE_SUBCLASS0 = 0
      TYPE_SUBCLASS1 = 1
  
      objects = ProductManager()
  
      def __init__(self, *a, **kwa):
          self.set_defaults()
  
      def set_defaults(self):
          pass
  
  
  class ProductSubClass0(models.Model):
      def set_defaults(self):
          self.type == self.TYPE_SUBCLASS0
  </code></pre>
  
  <p>...but I don't know how to do it "right".  Can someone shed some light here?</p>