I've run into the following problem with model inheritance, and I
can't find a reasonable way to solve it:
Let's say I have an Animal model, with a few subclasses: Lion, Tiger,
Zebra and so on. The Animal model is not abstract, because I want to
be able to, say, select all the animals in the database.
So now I want to go over a collection of animals and determine whether
each one is a Lion or a Tiger (or an Elephant, a Zebra and so on...)
If I do this:
animals = Animal.objects.all()
I get a list of objects of type Animal. Each one will have a .tiger
attribute or a .lion attribute according to its subclass, so
apparently it "knows" what subclass it belongs to. But I can find no
way of determining its subclass in a generic manner.
To clarify, I would like to be able to do something like this:
animals = Animal.objects.all()
for animal in animals:
animal_type = ...
print "I am a", animal_type
and get:
I am a Zebra
I am a Lion
etc.
Any ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---