<s> from django . db . models import Manager <EOL> from django . db . models . query import QuerySet <EOL> class InheritanceQuerySet ( QuerySet ) : <EOL> @ property <EOL> def _child_rels ( self ) : <EOL> return [ <EOL> rel <EOL> for rel in self . model . _meta . get_all_related_objects ( ) <EOL> if rel . field . rel . parent_link <EOL> ] <EOL> def iterator ( self ) : <EOL> for obj in super ( InheritanceQuerySet , self ) . iterator ( ) : <EOL> for rel in self . _child_rels : <EOL> if getattr ( obj , rel . get_cache_name ( ) , None ) is not None : <EOL> yield getattr ( obj , rel . var_name ) <EOL> break <EOL> else : <EOL> yield obj <EOL> class InheritanceManager ( Manager ) : <EOL> use_for_related_fields = True <EOL> def get_query_set ( self ) : <EOL> qs = InheritanceQuerySet ( self . model , using = self . _db ) <EOL> return qs . select_related ( * [ rel . var_name for rel in qs . _child_rels ] ) </s>
