Django 表 - 如果项目是唯一的则单行

问题描述 投票:0回答:1

如果“类别名称”是唯一的,是否有办法获取 HTML 中单行的所有金额?根据我的代码,“类别名称”不再是唯一的,因为它是按月份注释的。谢谢你

数据:

HTML 输出:我希望它们全部排成一行,因为它们是“汽车付款”

Views.py

class CashFlow(LoginRequiredMixin, AccountContextMixin, TemplateView):
    model = Transaction
    template_name = 'transaction_app/cf.html'
    context_object_name = 'transaction'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        user = self.request.user

        #-------Dropdown Begins---------
        #Get unique years for dropdown
        distinct_years = Transaction.objects.filter(user=user).dates('transaction_date', 'year')
        year_choices = [(date.year, date.year) for date in distinct_years]
        unique_years = list(set(year_choices))
        unique_years.sort()
        context['unique_years'] = unique_years
        #-------Dropdown Ends-----------

        #---Get Data From Dropdown Begins---------       
        selected_year = self.request.GET.get('selected_year')
        selected_year = int(selected_year) if selected_year else datetime.now().year
    
        selected_status = self.request.GET.get('selected_status')
        #---Get Data From Dropdown Ends-----------

        #----Get Data for Table Begins------------
        
        # Obtain distinct category names
        category_names = Transaction.objects.filter(
            user=user,
            transaction_date__year=selected_year,
            status=selected_status
        ).values_list('category__category_name', flat=True).distinct()

        # Aggregate data for each distinct category
        category_sums = []
        for category_name in category_names:
        # Aggregate total amount for each month within the specified year for the current category
            category_sum_by_month = Transaction.objects.filter(
                user=user,
                transaction_date__year=selected_year,
                status=selected_status,
                category__category_name=category_name
            ).annotate(
                month=ExtractMonth('transaction_date')
            ).values('month').annotate(
                total_amount=Sum('amount')
            )

            # Append category name and its monthly sums to category_sums list
            for item in category_sum_by_month:
                item['category__category_name'] = category_name
                category_sums.append(item)

            # Aggregate total amount for the current category
            category_sum = Transaction.objects.filter(
                user=user,
                transaction_date__year=selected_year,
                status=selected_status,
                category__category_name=category_name
            ).aggregate(
                total_amount=Sum('amount')
            )

            category_sum['category__category_name'] = category_name
            category_sums.append(category_sum)
            category_sums.append(category_sum_by_month)

            context['category_sums'] = category_sums
        #----Get Data for Table Begins------------
        return context

HTML模板

<table class="tbl-big" border="1">
    <thead>
        <tr>
            <th>Category Name</th>
            <th>Jan</th>
            <th>Feb</th>
            <th>Mar</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>
        {% for item in category_sums %}
        <tr>
            <td>{{item.category__category_name}}</td>
            
            <td>
            {% if item.month == 1%}
            {{item.total_amount}}
            {% endif %}
            </td>
            
            <td>
            {% if item.month == 2%}
            {{item.total_amount}}
            {% endif %}
            </td>

            <td>
            {% if item.month == 3%}
            {{item.total_amount}}
            {% endif %}
            </td>

            <td>{{item.total_amount}}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

数据输出:

{'month': 1, 'total_amount': Decimal('-1400.03000000000'),
'category__category_name': 'Car Payment'} {'month': 2, 'total_amount':
Decimal('-1400.03000000000'), 'category__category_name': 'Car
Payment'} {'total_amount': Decimal('-2800.06000000000'),
'category__category_name': 'Car Payment'} <QuerySet [{'month': 1,
'total_amount': Decimal('-1400.03000000000'),
'category__category_name': 'Car Payment'}, {'month': 2,
'total_amount': Decimal('-1400.03000000000'),
'category__category_name': 'Car Payment'}]>
python django django-tables2
1个回答
0
投票

我认为这里的主要问题是视图应该以可访问的方式提供数据。我们可以合作:

from collections import Counter, defaultdict, namedtuple

CategoryOverview = namedtuple('CategoryOverview', ['name', 'entries', 'total'])


class CashFlow(LoginRequiredMixin, AccountContextMixin, TemplateView):
    model = Transaction
    template_name = 'transaction_app/cf.html'
    context_object_name = 'transaction'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        selected_year = self.request.GET.get('selected_year')
        selected_year = (
            int(selected_year) if selected_year else datetime.now().year
        )
        items = defaultdict(dict)
        sum_per_month = (
            Transaction.objects.filter(
                user=user,
                transaction_date__year=selected_year,
                status=selected_status,
            )
            .values(
                month=ExtractMonth('transaction_date'),
                category_name=F('category__category_name'),
            )
            .annotate(total_amount=Sum('amount'))
            .order_by('category_name', 'month')
        )
        for record in sum_per_month:
            items[record['category_name']][record['month']] = record.total_amount
        context['summary'] = [
            CategorySummary(
                key, [entries.get(month) for month in range(1, 12)], sum(entries)
            )
            for key, entries in items.items()
        ]
        return context

然后我们可以很容易地渲染它:

<table class="tbl-big" border="1">
    <thead>
        <tr>
            <th>Category Name</th>
            <th>Jan</th>
            <th>Feb</th>
            <th>Mar</th>
            <th>Apr</th>
            <th>May</th>
            <th>Jun</th>
            <th>Jul</th>
            <th>Aug</th>
            <th>Sep</th>
            <th>Oct</th>
            <th>Nov</th>
            <th>Dec</th>
            <th>Sep</th>
            <th>Total</th>
        </tr>
    </thead>
    <tbody>
        {% for category in summary %}
        <tr>
            <td>{{category.name }}</td>
            
            {% for entry in category.entries %}<td>{{ entry }}</td>{% endfor %}
            <td>{{ item.total }}</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

这也将提高效率,因为我们执行 one 来获取所有交易,然后简单地按类别对它们进行排序。

© www.soinside.com 2019 - 2024. All rights reserved.