Django 详细信息视图运行 2x 数量的必要查询



我是 django 的新手,我已经熬了半夜,试图弄清楚为什么这个视图对配置文件和点运行两个 sql 查询。根据 Django 调试工具,运行 7 个 SQL 查询。

 1. SELECT...profile
 2. SELECT...points
 3. SELECT...profile
 4. SELECT...points
 5. SELECT...django_session
 6. SELECT...auth_user
 7. SELECT...posts

我不明白为什么个人资料和积分被击中两次?命中每个配置文件的时间和命中每个时间点的 SQL 堆栈跟踪是相同的:

配置文件的 SQL 堆栈跟踪:

1./Library/Python/2.7/site-packages/django/contrib/staticfiles/handlers.py in call(72) return self.application(environ, start_response)

点的 SQL 堆栈跟踪:

  1. /Library/Python/2.7/site-packages/django/contrib/staticfiles/handlers.py in call(72)返回自我应用程序(环境,start_response)
  2. /Users/jcarnegie/Documents/web/admin-site/userProfile/views.py in get_context_data(19)context["points"] = Points.objects.get(user_id = self.kwargs["pk"])

这是我的代码:

class UserProfile(generic.DetailView):
    template_name = 'userProfile/story.html'
    model = Profile
    context_object_name = 'profile'
    def get_context_data(self, **kwargs):
        context = super(UserProfile, self).get_context_data(**kwargs)
        context["points"] = Points.objects.get(user_id = self.kwargs["pk"])
        context["posts"] = Posts.objects.filter(user_id = self.kwargs["pk"]).prefetch_related('tags')

这是我的模板:

{% extends "core/base.html" %}
{% load url from future %}
{% block content %}
<div class="content">
    <div class="content-inner">
    <div class="story">
        <div class="story-left" id="left-story">
        <div class="img">
            <div>
            <img src="https://s3.amazonaws.com/pic-3123/{{profile.pic}}" />
        </div>
        </div>
        <h1>{{ profile.fname }} {{ profile.lname }}</h1>
        <p>{{ profile.title }}</p>
        <div class="details">
        <div>
            <span>Points</span>
            <p>{{ points.total }}</p>
        </div>
            {% if profile.city and profile.state %}
        <div>
        <span><i class="icon-map-marker"></i></span>
        <p>{{ profile.city }}, {{ profile.state }}</p>
            </div>
        {% endif %}
        {% if profile.company %}
        <div>
               <span><i class="icon-briefcase"></i></span>
           <p>{{ profile.company }}</p>
        </div>
        {% endif %}
        <div>
        <span><i class="icon-time"></i></span>
        <p><span class="muted">Joined on</span> {{ profile.dateJoined|date:"M d, Y" }}</p>
        </div>
    </div>
    </div>
    <div class="story-right">
       <h3>Posts Made</h3>
       <div class="tab-content">
       {% if posts %}
          <ul id="contributionHolder" class="right-ul">
              {% for post in posts %}
              <li class="content-item" id="post_{{post.id}}">
             <h1 class="volk-font"><a href="{% url 'contributions:detail' post.url post.id %}">{{post.title}}</a></h1>
             <p class="volk-font limited-text">{{ post.description }}</p>
             <div class="tag-holder">
                  {% for tag in post.tags.all %}
              <a class="tag volk-font grey-button-flat" href="">{{tag.name}} </a>
                      {% endfor %}
                     </div>
           </li>                        
           {% endfor %}
      </ul>
    {% else %}
        <div class="alert"> <button type="button" class="close" data-dismiss="alert">×</button>{{ profile.fname }} has not made any posts.</div>
    {% endif %}
    </div>
        </div>
        </div>
    </div>
</div>
{% endblock %}

任何帮助或想法将不胜感激!提前谢谢。

如果城市和州是配置文件模型的外键,则每次使用模板标签调用这些字段时,您都会命中数据库 {{ profile.city }}, {{ profile.state }} .但是,您可以通过使用select_related()方法缓存它来优化性能(请注意,它适用于任何具有外键的模型)。

最简单的方法是在视图中设置queryset属性而不是模型属性:

queryset = Profile.objects.select_related().all()

并查看有关此主题的 django 文档以获取更多详细信息:

https://docs.djangoproject.com/en/1.5/ref/models/querysets/

相关内容

最新更新