Django 表格 - 如果项目唯一则单行显示

1 投票
1 回答
71 浏览
提问于 2025-04-13 19:26

有没有办法把所有的金额放在一行里,如果“类别名称”是唯一的?根据我的代码,“类别名称”不再是唯一的,因为它被标注了月份。谢谢

数据:

在这里输入图片描述

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'}]>

解决方案:感谢@willeM_ Van Onsem

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)
        user = self.request.user
...
        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'] = [
            CategoryOverview(key, [entries.get(month) for month in range(1, 13)], sum(entries)
            )
            for key, entries in items.items()
        ]
        return context

1 个回答

1

我觉得这里主要的问题是视图应该以一种容易获取的方式提供数据。我们可以使用:

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']
        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>

这样做也会提高效率,因为我们只需要进行一次操作来获取所有的交易记录,然后再按类别简单地进行排序。

撰写回答