Commits

Daniel Patterson  committed 71b0d75

adding select_related_types and depth options to managers.InheritanceCastMixin.cast(), the former a dictionary with 'all' and all the subclasses as keys and lists of fields to select related on, and the latter being the depth argument to select_related.

  • Participants
  • Parent commits 85b1ef0

Comments (0)

Files changed (1)

File model_utils/managers.py

 
 
 class InheritanceCastMixin(object):
-    def cast(self):
+    def cast(self, select_related_types=None, depth=None):
         results = tuple(self.values_list('pk', 'real_type'))
         type_to_pks = {}
         for pk, real_type_id in results:
         for real_type_id, pks in type_to_pks.iteritems():
             content_type = content_types[real_type_id]
             child_type = content_type.model_class()
-            children = child_type._default_manager.in_bulk(pks)
+            if select_related_types:
+                params = select_related_types.get('all',[]) + select_related_types.get(content_type.model, [])
+                children = child_type._default_manager.select_related(*params).in_bulk(pks)
+            elif depth:
+                children = child_type._default_manager.select_related(depth=depth).in_bulk(pks)
+            else:
+                children = child_type._default_manager.in_bulk(pks)
             for pk, child in children.iteritems():
                 pk_to_child[pk] = child
         children = []