【问题标题】:Using distinct() with filter() in django在 django 中使用 distinct() 和 filter()
【发布时间】:2018-10-23 23:59:47
【问题描述】:

我正在尝试在 Django 中创建一个查询,该查询调用满足过滤器某些条件(使用 filter)的唯一行(使用 distinct

这里是使用的文件:

views.py

def cat_details(request, pk):
    current_user = request.user
    selected_cat = get_object_or_404(Category, pk=pk)
    selected_items = ItemIn.objects.all().filter(item_category=selected_cat).values_list('item_name', flat=True).distinct()
    all_cats = Category.objects.all()
    cat_count = all_cats.count()
    item_count = ItemIn.objects.values_list('item_name', flat=True).distinct().count()  # returns a list of tuples..
    #all_units = Item.objects.aggregate(Sum('item_quantity'))['item_quantity__sum']
    context = {
        #'all_units': all_units,
        'item_count': item_count,
        'cat_count': cat_count,
        'selected_items': selected_items,
        'selected_cat': selected_cat,
        'current_user': current_user,
    }

    return render(request, 'townoftech_warehouse/cat_details.html', context)

名为selected_items 的变量是问题所在!

这就是我如何使用这个视图函数

HTML

{% extends 'townoftech_warehouse/base.html' %}
    {% block content %}
{% load static %}
        <div class="card">
  <div class="card-header">
    الاصناف فى المخزن
  </div>
  <div class="card-body">
      <h3 align="right"> {{ selected_cat }}</h3>
      <footer><a href="{% url 'edit_cat' selected_cat.pk%}"><button type="button" class="btn btn-dark">تعديل إسم الفئة</button></a>
     <a href="{% url 'category_delete' selected_cat.pk%}"><button type="button" class="btn btn-dark">مسح الفئة</button></a>
</footer>
  </div>
</div>
                  <div style="overflow: auto;height: 280px">

        <table class="table table-bordered">

  <tbody>
  {% for item in selected_items %}
    <tr align="right">
        <th scope="row"><a href = "{% url 'items' item.pk%}">{{ item.item_name }}</a></th>
    </tr>
  {% endfor %}
  </tbody>
</table>
                  </div>
        <br>
        <br>
<div align="right"><a href="{% url 'cat' %}"><button type="button" class="btn btn-danger">رجوع</button></a></div>

{% endblock %}

每当我尝试打开此页面时,我得到的错误是:

错误

NoReverseMatch at /category/15/
Reverse for 'items' with arguments '('',)' not found. 1 pattern(s) tried: ['item\\/(?P<pk>[0-9]+)\\/$']

更新

我已经从 HTML 文件中删除了链接

变了

{% for item in selected_items %} {{ item.item_name }} {% endfor %}

变得像

  {% for item in selected_items %}
    <tr align="right">
        <th scope="row">{{ item.item_name }}</th>
    </tr>
  {% endfor %}

错误消失了,但它现在给了我一个空值列表!

【问题讨论】:

    标签: python django


    【解决方案1】:

    selected_items 变量包含字符串对象列表(项目名称)。字符串没有pk 属性,因此模板中的item.pk 不返回任何内容。

    你需要传递给对象的模板列表而不是values_list。如果您使用的是 postgres,您可以使用 distinct() 和参数。

    selected_items = ItemIn.objects.all().filter(item_category=selected_cat).distinct('item_name')
    

    否则你可以尝试使用values'item_name', 'pk':

    selected_items = ItemIn.objects.all().filter(item_category=selected_cat).values('item_name', 'pk').distinct()
    

    【讨论】:

    • 我正在使用 sqlite3,所以我不能像这样使用它,我有一个错误DISTINCT ON fields is not supported by this database backend
    • @AhmedWagdi 然后尝试values。检查更新。
    • 谢谢你,兄弟,它给了我没有更多的错误,但它仍然显示重复的条目!
    • @AhmedWagdi 是的,在这种情况下,distinct 适用于这两个领域。所以你最好转移到 Postgres,反正 SQlite 不是生产的好选择,检查这个问题:stackoverflow.com/questions/6913833/…
    猜你喜欢
    • 2011-04-13
    • 2017-09-08
    • 2012-06-29
    • 2014-09-11
    • 2019-06-26
    • 2021-04-13
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    相关资源
    最近更新 更多