Commits

Chris Moffitt  committed 22bb8e0

Adding some DB optimizations for Django 1.4. Closes #1446

  • Participants
  • Parent commits 15af2c3

Comments (0)

Files changed (2)

File satchmo/apps/product/models.py

 
     def _get_mainImage(self):
         img = False
-        if self.images.count() > 0:
-            img = self.images.order_by('sort')[0]
+        images = self.images.order_by('sort')
+        if images:
+            img = images[0]
         else:
             if self.parent_id and self.parent != self:
                 img = self.parent.main_image
 
         if variations:
             slugs = qry.filter(site=self.site, active=True, **kwargs).values_list('slug',flat=True)
-            return Product.objects.filter(Q(productvariation__parent__product__slug__in = slugs)|Q(slug__in = slugs))
+            return Product.objects.filter(Q(productvariation__parent__product__slug__in = slugs)|Q(slug__in = slugs)).prefetch_related('productimage_set')
         else:
-            return qry.filter(site=self.site, active=True, productvariation__parent__isnull=True, **kwargs)
+            return qry.filter(site=self.site, active=True, productvariation__parent__isnull=True, **kwargs).prefetch_related('productimage_set')
+ 
 
     def translated_attributes(self, language_code=None):
         if not language_code:

File satchmo/apps/satchmo_ext/recentlist/context_processors.py

     """Puts the recently-viewed products in the page variables"""
     recent = request.session.get('RECENTLIST',[])
     maxrecent = config_value('PRODUCT','RECENT_MAX')
-        
-    products = []
-    for slug in recent:
-        if len(products) > maxrecent:
-            break
-            
-        try:
-            p = Product.objects.get_by_site(slug__exact = slug)
-            products.append(p)
-        except Product.DoesNotExist:
-            pass
+    products = Product.objects.active_by_site().filter(
+        slug__in = recent[:maxrecent]).prefetch_related('productimage_set')    
+    return {'recent_products' : products}
     
-    return {'recent_products' : products}
-