Python 如何在django模板中生成包含两个外键链接模型的筛选摘要?

Python 如何在django模板中生成包含两个外键链接模型的筛选摘要?,python,django,django-templates,Python,Django,Django Templates,我目前正在学习django,以下是当前设置: models.py class Customer(models.Model): name = models.CharField(max_length=200, null=True) phone = models.CharField(max_length=200, null=True) def __str__(self): return self.name class Order(models.Model):

我目前正在学习django,以下是当前设置:

models.py

class Customer(models.Model):
    name = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=200, null=True)
    def __str__(self):
        return self.name

class Order(models.Model):
    STATUS = (
        ('Pending', 'Pending'),
        ('Delivered', 'Delivered'),
    )
    customer = models.ForeignKey(Customer, null=True, on_delete=models.SET_NULL)
    status = models.CharField(max_length=200, null=True, choices=STATUS, default="Pending")
    def __str__(self):
        return self.product.name
views.py

def home(request):
    orders = Order.objects.all().order_by('-date_created')
    customers = Customer.objects.all()
    c_pending = orders.filter(status='Pending').all()

    context = {
        'orders': orders,
        'customers': customers,
        'c_pending': c_pending,
    }
    return render(request, 'home.html', context)
home.html

{% for customer in customers %}
<tr>
    <td>{{customer.name}}</td>
    <td>{{customer.phone}}</td>
    <td>{{customer.order_set.count}}</td>
    <td>{{c_pending.count}}</td>
</tr>
{% endfor %}
{customers%中客户的%
{{customer.name}
{{customer.phone}}
{{customer.order_set.count}
{{c_pending.count}
{%endfor%}

除了第四栏外,第一栏到第三栏的作品都完美无缺。问题是我希望在第四列显示每个客户处于待定状态的订单数量,但它确实显示了整个待定订单的总数。为了django模板正确提取每个处于待定状态的客户的订单数量,我缺少哪些步骤。非常感谢您的帮助。谢谢

c_pending
只是所有挂起订单的查询集,而不是该客户本身。您可以使用待定订单的数量对查询集进行注释:

from django.db.models import Count, Q

def home(request):
    orders = Order.objects.all().order_by('-date_created')
    customers = Customer.objects.annotate(
        total_orders=Count('order'),
        total_pending=Count('order', filter=Q(order__status='Pending'))
    )

    context = {
        'orders': orders,
        'customers': customers,
        'c_pending': c_pending,
    }
    return render(request, 'home.html', context)
从django.db.models导入计数,Q
def home(请求):
orders=Order.objects.all().Order\u by('-date\u created'))
customers=Customer.objects.annotate(
订单总数=计数(“订单”),
总计\u挂起=计数('order',filter=Q(order\u status='pending'))
)
上下文={
"命令":命令,,
“客户”:客户,
“c_挂起”:c_挂起,
}
返回渲染(请求'home.html',上下文)
在视图中,然后使用以下选项渲染此对象:

{% for customer in customers %}
<tr>
    <td>{{ customer.name }}</td>
    <td>{{ customer.phone }}</td>
    <td>{{ customer.total_orders }}</td>
    <td>{{ customer.total_pending }}</td>
</tr>
{% endfor %}
{customers%中客户的%
{{customer.name}
{{customer.phone}
{{customer.total_orders}
{{customer.total_pending}

{%endfor%}
c_pending
只是所有挂起订单的查询集,而不是该客户本身。您可以使用待定订单的数量对查询集进行注释:

from django.db.models import Count, Q

def home(request):
    orders = Order.objects.all().order_by('-date_created')
    customers = Customer.objects.annotate(
        total_orders=Count('order'),
        total_pending=Count('order', filter=Q(order__status='Pending'))
    )

    context = {
        'orders': orders,
        'customers': customers,
        'c_pending': c_pending,
    }
    return render(request, 'home.html', context)
从django.db.models导入计数,Q
def home(请求):
orders=Order.objects.all().Order\u by('-date\u created'))
customers=Customer.objects.annotate(
订单总数=计数(“订单”),
总计\u挂起=计数('order',filter=Q(order\u status='pending'))
)
上下文={
"命令":命令,,
“客户”:客户,
“c_挂起”:c_挂起,
}
返回渲染(请求'home.html',上下文)
在视图中,然后使用以下选项渲染此对象:

{% for customer in customers %}
<tr>
    <td>{{ customer.name }}</td>
    <td>{{ customer.phone }}</td>
    <td>{{ customer.total_orders }}</td>
    <td>{{ customer.total_pending }}</td>
</tr>
{% endfor %}
{customers%中客户的%
{{customer.name}
{{customer.phone}
{{customer.total_orders}
{{customer.total_pending}

{%endfor%}
c_pending
只是所有未决订单的查询集,而不是该客户本身。
c_pending
只是所有未决订单的查询集,而不是该客户本身。非常感谢,威廉。现在它在所有栏目中都完美无瑕。非常感谢,威廉。现在,它在所有列中都能完美地工作。