Search code examples
djangoinheritancedjango-managers

Django inherited model instance creation function


Say I have the following, Model_1 has 1 integer, Model_2 inherits Model_1 and adds another integer.

class Model_1Manager (models.Manager):
  def create (self, i)
    m = self.model(value1=i)
    m.save()

class Model_1 (models.Model):
  value1 = model.IntegerField()
  objects = Model_1Manager()

class Model_2Manager (Model_1Manager):
  def create (self, i1, i2)
    m = self.model(value1=i1, value2=i2)
    m.save()

class Model_2 (Model_1):
  value2 = model.IntegerField()
  objects = Model_2Manager()

Questions:

  1. Is the create() function in Model_2Manager written correctly as is?
  2. Is there some way for me to call Model_1Manager's create() function?? How would that work?

Solution

  • Is the create() function in Model_2Manager written correctly as is?

    It is.

    Is there some way for me to call Model_1Manager's create() function?? How would that work?

    Unfortunately in this case Model_1Manager.create() does not do enough to initialize a Model_2, so you should not attempt to call it.