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

时间:2013-09-15 05:49:03

标签: python django django-views django-class-based-views

我是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在调用(72)中返回self.application(environ,start_response)

点的SQL堆栈跟踪:

  1. /Library/Python/2.7/site-packages/django/contrib/staticfiles/handlers.py in call (72) return self.application(environ,start_response)
  2. get_context_data中的
  3. /Users/jcarnegie/Documents/web/admin-site/userProfile/views.py(19) context [“points”] = Points.objects.get(user_id = self.kwargs [“pk”])
  4. 这是我的代码:

    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 %}
    

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

1 个答案:

答案 0 :(得分:0)

如果城市和州是您的个人资料模型的外键,那么每次使用模板标记 {{ profile.city }}, {{ profile.state }}调用这些字段时,您都会点击数据库。但是,您可以通过使用select_related()方法缓存来优化性能(请注意,它适用于具有外键的任何模型)。

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

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

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

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