queries optimisation second step
authorcavaliet
Thu, 04 Jul 2013 12:13:42 +0200
changeset 218 87fd3589b65a
parent 217 f0883894f386
child 219 6667fb5455d0
child 220 c38fbfea55ac
queries optimisation second step
src/egonomy/models.py
src/egonomy/templates/partial/collection_in_list.html
src/egonomy/utils/queries.py
--- a/src/egonomy/models.py	Thu Jul 04 11:31:21 2013 +0200
+++ b/src/egonomy/models.py	Thu Jul 04 12:13:42 2013 +0200
@@ -9,6 +9,7 @@
 from django.contrib.auth.models import User
 from django.contrib.contenttypes.models import ContentType
 from django.contrib.contenttypes import generic
+from egonomy.utils.queries import cache_generics
 
 class ImageMetadata(models.Model):
     
@@ -196,6 +197,12 @@
     modification = models.DateTimeField(auto_now=True)
     public = models.BooleanField(null=False, default=True) # Collection is published or not, always published by default
     publication_type = models.IntegerField(choices=PUBLICATION_CHOICES, default=1) # list, mosaic, slideshow or geographical
+    
+    @property
+    def first_four_items(self):
+        items = CollectionItem.objects.filter(collection=self).select_related('author', 'content_type', 'object_id', 'content_object').order_by("order")[:4]
+        cache_generics(items)
+        return items
 
 
 
--- a/src/egonomy/templates/partial/collection_in_list.html	Thu Jul 04 11:31:21 2013 +0200
+++ b/src/egonomy/templates/partial/collection_in_list.html	Thu Jul 04 12:13:42 2013 +0200
@@ -10,7 +10,7 @@
     </ul>
 </div>
 <a href="{% url 'view_collection' collection_pk=col.pk %}">
-{% with items=col.items.all %}
+{% with items=col.first_four_items %}
 <div class="top clearfix">
   {% for item in items|slice:":2" %}
     {% include 'partial/little_square_item_for_collection.html' %}
--- a/src/egonomy/utils/queries.py	Thu Jul 04 11:31:21 2013 +0200
+++ b/src/egonomy/utils/queries.py	Thu Jul 04 12:13:42 2013 +0200
@@ -8,7 +8,6 @@
 Adapted for egonomy project by Thibaut Cavalie
 '''
 from django.contrib.contenttypes.models import ContentType
-from egonomy.models import Image, Fragment
 
 import logging
 logger = logging.getLogger(__name__)
@@ -24,10 +23,12 @@
     for ct, fk_list in generics.iteritems():
         ct_model = content_types[ct].model_class()
         # In our case relations[ct] = ct_model.objects.in_bulk(list(fk_list))
-        # is not enough because we need select_related for both images and fragments
-        if content_types[ct] == ContentType.objects.get_for_model(Image):
+        # is not enough because we need select_related for both images and fragments.
+        # We test str(content_types[ct])=="fragment" because it avoids import Image and Fragment just to test contenttypes.
+        # So we can import this function in models.py.
+        if str(content_types[ct]) == "image":
             relations[ct] = ct_model.objects.select_related('info', 'metadata').in_bulk(list(fk_list))
-        elif content_types[ct] == ContentType.objects.get_for_model(Fragment):
+        elif str(content_types[ct]) == "fragment":
             relations[ct] = ct_model.objects.select_related('image', 'image__info', 'image__metadata','author').in_bulk(list(fk_list))
         else:
             relations[ct] = ct_model.objects.in_bulk(list(fk_list))