I'm having a problem with multi-table inheritance in django.
Let’s make an example with bank accounts.
class account(models.Model):
name = models……
class accounttypeA(account):
balance = models.float…..
def addToBalance(self, value):
self.balance += value
class accounttypeB(account):
balance = models.int…. # NOTE this
def addToBalance(self, value):
value = do_some_thing_with_value(value) # NOTE this
self.balance += value
Now, i want to add a value to an accounttype, but all i have is an account object, for instance acc=account.object.get(pk=29) . So, who is the child of acc ?
Django automatically creates an account_ptr_id field in accounttypeA and accounttypeB. So, my solution was:
child_class_list = ['accounttypeA', 'accounttypeB']
for cl in child_class_list:
try:
exec(“child = ” + str(cl) + “.objects.select_for_update().get(account_ptr_id=” + str(acc.id) + “)”)
logger.debug(“Child found and ready to use.”)
return child
except ObjectDoesNotExist:
logger.debug(“Object does not exist, moving on…”)
Maybe it's a drawing board problem at this point! :)
I hope I have been clear in my example. Thanks