. type . , , :
for animal in animals:
if (animal.type == "cat"):
animal_proxy =
Django. ( ).
, :
"" -. , Dog.objects Animal type="dog" Cat.objects Animal type="cat".
class TypeAwareManager(models.Manager):
def __init__(self, type, *args, **kwargs):
super(TypeAwareManager, self).__init__(*args, **kwargs)
self.type = type
def get_query_set(self):
return super(TypeAwareManager, self).get_query_set().filter(
type = self.type)
class Dog(Animal):
objects = TypeAwareManager('dog')
...
class Cat(Animal):
objects = TypeAwareManager('cat')
...
-, . . itertools.chain Querysets.
from itertools import chain
q1 = Cat.objects.all()
q2 = Dog.objects.all()
for each in chain(q1, q2):
each.make_noise()