Python 使用for循环创建多个注释

Python 使用for循环创建多个注释,python,django,Python,Django,这是我的密码: phones = Customer.objects.filter(active=True).values('name')\ .annotate(count = Count('phone',filter=Q(phone__model__icontains=model_list[0])) .annotate(count1 = Count('phone',filter=Q(phone__model__icontains=model_list[1]))

这是我的密码:

 phones = Customer.objects.filter(active=True).values('name')\
        .annotate(count = Count('phone',filter=Q(phone__model__icontains=model_list[0]))
        .annotate(count1 = Count('phone',filter=Q(phone__model__icontains=model_list[1]))
        .annotate(count2 = Count('phone',filter=Q(phone__model__icontains=model_list[2]))
        .annotate(count3 = Count('phone',filter=Q(phone__model__icontains=model_list[3]))
        .annotate(count4 = Count('phone',filter=Q(phone__model__icontains=model_list[4]))
    ........
html

{% if phones %}
    {% for phone in phones %}
    <tr>
        <td>{{ phone.name }}</td>
        <td>{{ phone.count  }}</td>
        <td>{{ phone.count1  }}</td>
        <td>{{ phone.count2  }}</td>
        <td>{{ phone.count3  }}</td>
        <td>{{ phone.count4  }}</td>
    </tr>
    {% endfor %}
{% enfif %}
{% if phones %}
{% for phone in phones %}
<tr>
    <td>{{ phone.name }}</td>
    <td>{{ phone.count  }}</td>
</tr>
{% endfor %}
{% endif %}
html

{% if phones %}
    {% for phone in phones %}
    <tr>
        <td>{{ phone.name }}</td>
        <td>{{ phone.count  }}</td>
        <td>{{ phone.count1  }}</td>
        <td>{{ phone.count2  }}</td>
        <td>{{ phone.count3  }}</td>
        <td>{{ phone.count4  }}</td>
    </tr>
    {% endfor %}
{% enfif %}
{% if phones %}
{% for phone in phones %}
<tr>
    <td>{{ phone.name }}</td>
    <td>{{ phone.count  }}</td>
</tr>
{% endfor %}
{% endif %}
{%if%}
{%用于电话中的电话%}
{{phone.name}
{{phone.count}
{%endfor%}
{%endif%}
但是结果不是我想要的,因为我只得到一个数据。
例如:
模型列表[0]
我不是django专家,但我认为这是您的错误:

phone = Customer.objects.filter(active=True).values('name')

for i in range(len(model_list)):
     phone= phone.annotate(count = Count('phone',filter=Q(phone__model__icontains=model_list[i]))
您总是使用for循环中的最后一个值覆盖手机,请尝试使用此值

phones = Customer.objects.filter(active=True).values('name')\
    .annotate(count = Count('phone',filter=Q(phone__model__icontains=[i for i in model_list]))

执行以下操作以查询计数:

phones = Customer.objects.filter(active=True).values('name')

for idx, model in enumerate(model_list):
    counts = {'count%s' % idx : Count('phone',filter=Q(phone__model__icontains=model)}
    phones = phones.annotate(**counts)
然后您需要将一系列模型传递给模板(比如在
models\u idx\u range
中作为上下文参数
models\u idx\u range=range(models\u count)
)并迭代计数:

{% if phones %}
{% for phone in phones %}
<tr>
    <td>{{ phone.name }}</td>
    {% for idx in models_idx_range %}
        <td>{{ getattr(phone, 'count%s' % idx) }}</td>
</tr>
{% endfor %}
{% endif %}
{%if%}
{%用于电话中的电话%}
{{phone.name}
{idx型号中的idx百分比\ idx范围%}
{{getattr(电话,'count%s'%idx)}
{%endfor%}
{%endif%}

我应该如何在html中显示它?这不起作用。如果将列表传递给
\uu icontains
,则查询将找不到任何内容。这将不起作用,因为这将仅用一个聚合替换
N
聚合(
count
count1
等)。我收到一个错误。
在字符串格式化过程中未转换所有参数。此错误指向行
counts={……
,原因是,
'count%s'%idx
中有一个输入错误。现在修复了我知道您的代码是如何工作的,但我收到了此错误:
无法解析其余部分:'(models\u count)'from'range(models\u count)“
看来django模板需要改进,因此您需要一个解决方案。我已经更新了答案