I receive on SQL request for each element images. If you remove {% if img.categories == category%} in the template, then the number of requests is normal, but how then to figure them out is not entirely clear. Tried to use {% regroup by as%} in the template, but then the same large number of queries ... Help optimize this outrage ... Thank you.
In models.py
class Catalog(models.Model): name = models.CharField(max_length=100) def __unicode__(self): return self.name class Categories(models.Model): name = models.CharField(max_length=200) catalog = models.ForeignKey(Catalog) def __unicode__(self): return self.name class Images(models.Model): name = models.CharField(max_length=200) img = ResizedImageField(upload_to='catalog') categories = models.ForeignKey(Categories) In views.py
def catalogManufacturer(request, url): args = {} args['catalog'] = Catalog.objects.get(url=url) args['categories'] = Categories.objects.filter(catalog=args['catalog'].id) args['images'] = Images.objects.filter(categories=args['categories']) return render_to_response('catalog-manufacturer.html', args) Well, the template.
{% for category in categories %} <h2>{{category.name}}</h2> <ul> {% for img in images %} {% if img.categories == category %} <li> <a href="{% cropped_thumbnail img "cropping" scale=1 %}"> <img src="{% cropped_thumbnail img "cropping" scale=0.6 %}" alt="{{img.name}}"> </a> {{img.name}} </li> {% endif %} {% endfor %} </ul> {% endfor %}
cropped_thumbnail(not the fact that it affects, but just in case) - andreymalCategoryandImage- andreymal