在Django视图中多次查询同一个对象

时间:2016-06-12 21:07:32

标签: django

如果我想查询整个字母表中的所有用户,我可以在视图中执行此操作,还是会遇到很多性能问题?

users_a = User.objects.filter(username__startswith="a")
users_b = User.objects.filter(username__startswith="b")
users_c = User.objects.filter(username__startswith="c")
users_d = User.objects.filter(username__startswith="d")
users_e = User.objects.filter(username__startswith="e")

澄清我想要达到的目标:

我的观点:

def site_list(request):
    from django.contrib.auth.models import User
    users_a = User.objects.filter(username__startswith="a")
    return render(request, 'lists/sites.html', {'users_a': users_a})

我的模板:

<div class="col-md-12">
<h3>A</h3></div>
{% for user in users_a %}
  <div class="col-md-3">
    <a href="{% if request.is_secure %}https{% else %}http{% endif %}://{{ user.username }}.{{ request.get_host }}">
      <img class="avatar" src="{% avatar_url user 60 %}" alt="#">
      <span class="title">{{ user.first_name }} {{ user.last_name }}</span>
      <span class="url">{{ user.username }}.domain.com</span>
    </a>
  </div>
{% endfor %}

0 个答案:

没有答案
相关问题