class Contact(models.Model): content_type = models.ForeignKey(ContentType,editable=False,null=True)
def save(self): if(not self.content_type): self.content_type = ContentType.objects.get_for_model(self.__class__) self.save_base() def as_leaf_class(self): content_type = self.content_type model = content_type.model_class() if(model == Contact): return self return model.objects.get(id=self.id) note that this would reload the object from database. if you merely want to find out what animal it is, then you can just check animal.content_type use a debug middleware footer so that you can check what queries are actually performed. http://www.djangosnippets.org/snippets/1031/ On Sep 7, 1:54 pm, dozo <[EMAIL PROTECTED]> wrote: > 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 django-users@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---