Commits

Mikhail Korobov committed 29e726c

Support for select_related in RelatedInjector and GenericInjector

Comments (0)

Files changed (2)

generic_utils/managers.py

         self.fk_field = fk_field
         super(RelatedInjector, self).__init__(*args, **kwargs)
 
-    def inject_to(self, objects, field_name, get_inject_object = lambda obj: obj,  **kwargs):
+    def inject_to(self, objects, field_name, get_inject_object = lambda obj: obj,
+                  select_related = None, **kwargs):
         '''
         ``objects`` is an iterable. Related objects
             will be attached to elements of this iterable.
             result of ``get_inject_object(obj)``. It is assumed that ``fk_field``
             points to  ``get_inject_object(obj)``.
 
+        ``select_related`` is a list to be passed to select_related method for
+            related objects.
+
         All other kwargs will be passed as arguments to queryset filter function.
 
         For example, we need to prefetch user profiles when we display a list of
         kwargs.update({self.fk_field+'__in': [ get_inject_object(obj).pk for obj in objects ]})
 
         data = self.get_query_set().filter(**kwargs)
+        if select_related:
+            data = data.select_related(select_related)
+
         data_dict = dict((getattr(item, self.fk_field), item) for item in list(data))
 
         # add info to original data
 
 setup(
       name='django-generic-images',
-      version='0.36',
+      version='0.36.1',
       author='Mikhail Korobov',
       author_email='kmike84@gmail.com',
       url='http://bitbucket.org/kmike/django-generic-images/',