Chris Moffitt avatar Chris Moffitt committed 22bb8e0

Adding some DB optimizations for Django 1.4. Closes #1446

Comments (0)

Files changed (2)

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:

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}
-    
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.