Skip to content

Instantly share code, notes, and snippets.

@pelletier
Created July 2, 2011 09:42
Show Gist options
  • Save pelletier/1059902 to your computer and use it in GitHub Desktop.
Save pelletier/1059902 to your computer and use it in GitHub Desktop.
# I have (models.py)
from django.db import models
class ParentModel(models.Model):
field_a = ...
field_b = ...
def a_method(self):
pass
class ChildModelA(ParentModel):
field_c = ...
def a_method(self):
print "A"
def b_method(self):
print self.field_c
class ChildModelB(ParentModel):
field_d = ....
def a_method(self):
print "B"
def b_method(self):
print self.field_d
# What I'd like to do:
>>> obj = ParentModel.objects.get(pk=n) # Same thing with filter as well
>>> obj.a_method()
# If it happens that obj is a ChildA instance:
"A"
# If it's a ChildB instance:
"B"
>>> obj.b_method()
# The correct result here according to obj's class.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment