从属下拉链django过滤器

时间:2019-05-05 09:59:58

标签: django django-filter

如何在两个字段上获取从属过滤器?

models.py

class FinanceAccountDetail(models.Model):
    accountName = models.CharField(max_length=40)
    msdmName = models.CharField(max_length=80)
    deliveryManagedFrom = models.CharField(max_length=40)
    marketName = models.CharField(max_length=20)
    isActive = models.BooleanField(default=True)
    year = models.IntegerField ( default=2019 )
    sdu_id = models.IntegerField ( default=1)

    def __str__(self):
        return u'%s' % (self.accountName)

filters.py

class dashboard_filter(django_filters.FilterSet):
    deliveryManagedFrom = django_filters.ModelChoiceFilter(queryset=FinanceAccountDetail.objects.values_list('deliveryManagedFrom',flat=True).distinct())
    accountName = django_filters.ModelChoiceFilter(queryset=FinanceAccountDetail.objects.values_list('accountName',flat=True).distinct())

    class Meta:
        model = FinanceAccountDetail
        fields = ['accountName', 'deliveryManagedFrom']

我要过滤,然后用这些过滤器制作查询集

views.py

def Dashboard(request , *args , **kwargs):

    q = {k: v for k , v in request.GET.items () if v}
    total_budget_usd_queryset = FinanceAccountDetail.objects.filter(**q).aggregate(budget=Sum(F('budget__january' ) + F ( 'budget__february' ) + F ( 'budget__march' ) + F ( 'budget__april' ) + F ( 'budget__may' ) + F ( 'budget__june' ) + F ('budget__july' ) + F ( 'budget__august' ) + F ( 'budget__september' ) + F ( 'budget__october' ) + F ( 'budget__november' ) + F ( 'budget__december' ) ) )
    total_usd_budget = total_budget_usd_queryset['budget']
    month_query = Hours.objects.values_list ( 'month' ).distinct ()
    month_final = [item for sublist in month_query for item in sublist]
    month = month_final[0]
    total_sek_budget = total_budget_usd_queryset['budget']
    total_actual_usd_queryset = FinanceAccountDetail.objects.filter(**q ).aggregate(actual=Sum (F ( 'budget__Carren_Jan_Cost' ) + F ( 'budget__Carren_Feb_Cost' ) + F ( 'budget__Carren_Mar_Cost' ) + F ( 'budget__Carren_Apr_Cost' ) + F ('budget__Carren_May_Cost' ) + F ( 'budget__Carren_Jun_Cost' ) + F ( 'budget__Carren_Jul_Cost' ) + F ( 'budget__Carren_Aug_Cost' ) + F ('budget__Carren_Sep_Cost' ) + F ( 'budget__Carren_Oct_Cost' ) + F ( 'budget__Carren_Nov_Cost' ) + F ( 'budget__Carren_Dec_Cost' ) ) )
    total_usd_actual = total_actual_usd_queryset['actual']
    total_budget_hours_queryset = FinanceAccountDetail.objects.filter(**q ).aggregate ( budget=Sum (F ('hours__january' ) + F ( 'hours__february' ) + F ( 'hours__march' ) + F ( 'hours__april' ) + F ( 'hours__may' ) + F ( 'hours__june' ) + F ('hours__july' ) + F ( 'hours__august' ) + F ( 'hours__september' ) + F ( 'hours__october' ) + F ( 'hours__november' ) + F ( 'hours__december' ) ) )
    total_hours_budget = total_budget_hours_queryset['budget']
    total_actual_hours_queryset = FinanceAccountDetail.objects.filter ( **q ).aggregate ( actual=Sum (F ('hours__CADO_Hour_Jan' ) + F ( 'hours__CADO_Hour_Feb' ) + F ( 'hours__CADO_Hour_Mar' ) + F ( 'hours__CADO_Hour_Apr' ) + F ('hours__CADO_Hour_May' ) + F ( 'hours__CADO_Hour_Jun' ) + F ( 'hours__CADO_Hour_Jul' ) + F ( 'hours__CADO_Hour_Aug' ) + F ('hours__CADO_Hour_Sep' ) + F ( 'hours__CADO_Hour_Oct' ) + F ( 'hours__CADO_Hour_Nov' ) + F ( 'hours__CADO_Hour_Dec' ) ) )
    total_hours_actual = total_actual_hours_queryset['actual']
    total_budget_usd_market_queryset = FinanceAccountDetail.objects.filter(**q).exclude(marketName='NA',marketName__exact='').values_list('marketName').distinct().annotate(budget=Sum(F('budget__january') + F('budget__february')+F('budget__march')+F('budget__april' ) + F ( 'budget__may' ) + F ( 'budget__june' ) + F ('budget__july' ) + F ( 'budget__august' ) + F ( 'budget__september' ) + F ( 'budget__october' ) + F ('budget__november')+F('budget__december' )))
    total_budget_usd_market_budget = [entry for entry in total_budget_usd_market_queryset]
    market = []
    budget =[]
    for i,j in total_budget_usd_market_budget:
        market.append(i)
        budget.append(j)

    a_zip = zip(market,budget)
    network_list = FinanceAccountDetail.objects.all ()
    network_filter = dashboard_filter( request.GET , request=request , queryset=network_list )
    return render ( request , 'finance_new/dashboard_summary.html' ,
                    {'filter': network_filter,'total_usd_budget':total_usd_budget,'total_usd_actual':total_usd_actual,
                    'total_hours_budget':total_hours_budget,'total_hours_actual':total_hours_actual,'total_sek_budget':total_sek_budget,
                     'a_zip':a_zip} )

HTML代码

{% extends 'finance_new/finance_base.html' %}

 {% load widget_tweaks %}
{% load humanize %}
 {% block body %}
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.8.1/css/all.css" integrity="sha384-50oBUHEmvpQ+1lW4y57PTFmhCaXp0ML5d60M1M7uH2+nqUivzIebhndOJK28anvf" crossorigin="anonymous">
       <div class="main-body">
        <div class="page-wrapper">
            <div class="page-header">
                <div class="page-header-title">
                    <h4>Financial Dashboard</h4>
                </div>
                <div class="page-header-breadcrumb">
                    <ul class="breadcrumb-title">
                        <li class="breadcrumb-item">
                            <a href="/finance_new/financemonthlyIndex/">
                                <i class="icofont icofont-home"></i>
                            </a>
                        </li>
                        <li class="breadcrumb-item">Dasboard
                        </li>
                        <li class="breadcrumb-item">Financial Dashboard
                        </li>
                    </ul>
                </div>
            </div>
          <div class="container" style="max-width: 1298px";>
          <form method="get" novalidate>
                <div class="well" style="border-radius: 20px;border-color: black;/* margin-left: -42px; *//* background-color: cadetblue; */width: 100%;">
                    <div class="row" >
                        <div  class="form-group col-sm-4 col-md-3">
                            SDU
                            {% render_field filter.form.deliveryManagedFrom class="form-control" %}
                        </div>

                        <div  class="form-group col-sm-4 col-md-3">
                            Account Name
                            {% render_field filter.form.accountName class="form-control" %}
                        </div>
                       <button type="submit" class="btn btn-primary" style="margin: 19px;">
                        <span class="glyphicon glyphicon-search"></span> Search
                    </button>
                    </div>
                </div>
          </form>

我被困住了。请注意,我要创建一个过滤器,而不是表单。

0 个答案:

没有答案