使用django-filter与django-tables2,如何注释查询集?

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

我使用django-filter和django-tables2从SQLite3数据库中提取数据。该数据库有15个以上的销售订单信息字段,它们可能是每个销售订单的多行(行字段)。我想渲染一个相当于'Group By'销售订单的表并将ExtAmount字段求和。我知道我应该使用annotate函数,但我不确定在哪里用django-filter / django-tables2实现它。

在SQL中我使用下面的内容来获得我需要的东西:

SELECT SalesOrder, Customer, Count(Line) AS Lines, Round(Sum(ExtAmount), 2) AS Amount FROM backlog_backlogData GROUP BY SalesOrder Order By Sum(ExtAmount) DESC limit 100

截至目前,该视图呈现了tables.py中的三个字段,但没有对SalesOrder进行分组。这很好,但我似乎无法找到需要注释的地方。我已经阅读了文档并搜索了其他问题,我似乎无法理解如何使用django-filter / django-table2。我在django-filter文档中引用'annotate'的唯一地方是使用ModelMultipleChoiceFilter过滤器引用。任何帮助都会很棒!

#models.py

from django.db import models

class backlogData(models.Model):
    Shipto = models.IntegerField()
    Customer = models.CharField(max_length=200)
    City = models.CharField(max_length=200, null=True)
    ST =  models.CharField(max_length=200, null=True)
    GLCo =  models.CharField(max_length=200)
    Parent = models.IntegerField()
    SalesOrder = models.IntegerField()
    CustomerPO =  models.CharField(max_length=200, null=True)
    OrderTyp =  models.CharField(max_length=200)
    Line = models.DecimalField(max_digits=20, decimal_places=1)
    ItemNumber = models.CharField(max_length=200, null=True)
    QtyOrdered = models.IntegerField()
    ExtAmount = models.DecimalField(max_digits=20, decimal_places=2)
    Truck = models.CharField(max_length=200, null=True)
    Stop = models.CharField(max_length=200, null=True)
    ShipClass = models.CharField(max_length=200, null=True)


#tables.py

import django_tables2 as tables
from .models import backlogData

class BacklogTable(tables.Table):

    class Meta:
        model = backlogData
        template_name = 'django_tables2/table.html'
        attrs = {'class': 'table is-fullwidth has-text-centered'}
        fields = ('GLCo','Customer','Truck')


#filters.py

import django_filters
from .models import backlogData 

class BacklogListFilter(django_filters.FilterSet):

    class Meta:
        model = backlogData
        fields = ['GLCo','Truck'] #used to filter after GET


#views.py

from django.views.generic import ListView
from django.db.models.query_utils import Q
from django_tables2 import RequestConfig
from django_filters.views import FilterView

from .utils import PagedFilteredTableView
from .models import backlogData
from .tables import BacklogTable
from .filters import BacklogListFilter

class BacklogListView(FilterView):
    model = backlogData
    template_name = 'backlog/index.html'
    filter_class = BacklogListFilter

    def get_context_data(self, **kwargs):
        context = super(BacklogListView, self).get_context_data(**kwargs)
        filter = BacklogListFilter(self.request.GET, queryset=self.get_queryset())
        table = BacklogTable(filter.qs)
        RequestConfig(self.request).configure(table)
        context['filter'] = table
        context['table'] = table
        return context
python django sqlite django-filter django-tables2
1个回答
0
投票

您可以将注释添加到传递给BacklogTable的查询集中:

table = BacklogTable(filter.qs.annotate(...))

有关如何使用.annotate() in django的文档。您可以使用注释的名称(通过将它们作为.annotate()的关键字参数提供)作为django-tables2中的列名。

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