[Answered ]-How to make a complex query in Django?

1๐Ÿ‘

โœ…

I have created a pages app. look where is the template.

views.py

from django.shortcuts import render
from .models import Appeal
from django.db.models import Count


def index(request):
    qs = Appeal.objects.values(
        "organization__organization_name", "appeal_form__form_name"
    ).annotate(total=Count("appeal_form__form_name"))
    context = {
        "appeal": qs,
    }
    return render(request, "pages/index.html", context)

index.html

<table>
    <tr>
        <th>Organization</th>
        <th>Appeal Form</th>
        <th>Amount of appeals in this form</th>
    </tr>
{% for item in appeal %}
<tr>
    <td>{{ item.organization__organization_name }}</td>
    <td>{{ item.appeal_form__form_name }}</td>
    <td>{{ item.total }}</td>
</tr>
{% endfor %}
</table>
pages
โ”œโ”€โ”€ __init__.py
โ”œโ”€โ”€ admin.py
โ”œโ”€โ”€ apps.py
โ”œโ”€โ”€ migrations
โ”œโ”€โ”€ models.py
โ”œโ”€โ”€ templates
โ”‚   โ””โ”€โ”€ pages
โ”‚       โ””โ”€โ”€ index.html
โ”œโ”€โ”€ tests.py
โ”œโ”€โ”€ urls.py
โ””โ”€โ”€ views.py

๐Ÿ‘คA D

Leave a comment