Django数据库访问优化
stevezhou 2015.01.16
一篇Django数据库访问优化的总结文章:http://www.the5fire.com/django-database-access-optimization.html
使用with模板标签
在模板中使用QuerySet缓存,需要使用with标签
{% with total=business.employees.count %}
{{ total }} employee{{ total|pluralize }}
{% endwith %}
预加载数据
QuerySet.select_related() , 针对foreign key 和 one-to-one
QuerySet.prefetch_related(),针对many-to-many
c=Classify.objects.filter(name__icontains=clyname).prefetch_related('keyword')