码迷,mamicode.com
首页 > 其他好文 > 详细

django的小操作,查询

时间:2018-07-12 16:37:05      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:use   请求   sel   for   cts   查询   ted   外键   django   

newses = News.objects.select_related(‘category‘, ‘author‘)  # category和author字段为News表的外键, 使用select_related查询,会等待前端请求完毕后一次性查询出所有的数据

前端:
{% for news in newses %}
  {{ news.category.name }}
  {{ news.author.username }} # 这两个字段为外键
{% endfor %}

这样能减少查询的次数,提高效率

django的小操作,查询

标签:use   请求   sel   for   cts   查询   ted   外键   django   

原文地址:https://www.cnblogs.com/tangpg/p/9299415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!