I'm having the same issue described in some places around the web and
also here:

http://stackoverflow.com/questions/5225556/determining-django-model-instance-types-after-a-query-on-a-base-class

In essence, I have these definitions:

class Animal(models.Model):
    name= models.CharField(max_length=128)

    def hello(self):
           print "Hello animal"

class Person(Animal):
    pants_size = models.IntegerField(null=True)

    def hello(self):
           print "Hello Mr"

class Dog(Animal):
    panting_rate = models.IntegerField(null=True)

    def hello(self):
           print  "Woof"

And create these instances...

Person(name='Dave').save()
Dog(name='Mr. Rufflesworth').save()

I do a Animal.objects.all(), and would like to run a method (.hello)
on what I received. A virtual method on the base class doesn't act as
expected and returns "Hello Animal" all the time.

I've seen the answers above and wondering if this is the right way to
go. I've noticed the returned instance has .dog attribute that doesn't
exists if this is a person or an animal, and a .person attribute that
doesn't exist if this is a dog or an animal.

Wouldn't going:

hasattr(result,'dog')

be cheaper (no db access)?

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to