djangoadmin后台搜索结果筛选自定义模版

django-admin对搜索结果进行自定义统计,可参考代码如下:

def changelist_view(self, request, extra_context=None):
        #cur1_time = datetime.now()
        data_dict = {}
        value = request.GET.get('q', "")
        bill_cycle = request.GET.get('bill_cycle__exact', "")
        resource = request.GET.get('resource__id__exact', '')
        date_year = request.GET.get('success_time__year', '')
        date_month = request.GET.get('success_time__month', '')
        date_day = request.GET.get('success_time__day', '')
        new_user = request.GET.get('new_old__exact', '') 
        source = request.GET.get('source__id__exact', None)
        kefu=request.GET.get('member_name__kefu__id__exact','0')
        #此处如果没选客服,则赋值0,下面条件根据赋值来进行数据统计,此参考代码只是临时解决并非最优
        if value:
            data_dict["product__name__icontains"] = value

        if bill_cycle:
            data_dict["bill_cycle__icontains"] = bill_cycle

        if resource:
            data_dict["resource__id__icontains"] = resource

        if source:
            data_dict["source__id__icontains"] = source

        if date_year:
            data_dict["success_time__year__icontains"] = date_year
        if date_month:
            data_dict["success_time__month__icontains"] = date_month
        if date_day:
            data_dict["success_time__day__icontains"] = date_day

        if request.user.groups.filter(name="销售部"):
            if new_user == "True":
                #模型中的new_old是bool类型,request得到的是str,所以此处只是偷懒的解决方法,根据if写死
                total = models.MoneyLog.objects.filter(Q(**data_dict), new_old=True, member_name__kefu=request.user,
                                                       ).annotate(month=TruncMonth("success_time")).values("month").annotate(c=Sum('money')).values("month", "c")
            elif new_user == "False":
                total = models.MoneyLog.objects.filter(Q(**data_dict), new_old=False, member_name__kefu=request.user,
                                                       ).annotate(month=TruncMonth("success_time")).values("month").annotate(c=Sum('money')).values("month", "c")
            else:
                total = models.MoneyLog.objects.filter(Q(**data_dict), member_name__kefu=request.user
                                                       ).annotate(month=TruncMonth("success_time")).values("month").annotate(c=Sum('money')).values("month", "c")
            print(total)
        else:
            if new_user == "True":
                if kefu=='0':
                    total = models.MoneyLog.objects.filter(Q(**data_dict), new_old=True, 
                                                       ).annotate(month=TruncMonth("success_time")).values("month").annotate(c=Sum('money')).values("month", "c")
                else:
                    total = models.MoneyLog.objects.filter(Q(**data_dict), new_old=True, member_name__kefu=kefu
                                                       ).annotate(month=TruncMonth("success_time")).values("month").annotate(c=Sum('money')).values("month", "c")
            elif new_user == "False":
                if kefu=='0':
                    total = models.MoneyLog.objects.filter(Q(**data_dict), new_old=False, 
                                                       ).annotate(month=TruncMonth("success_time")).values("month").annotate(c=Sum('money')).values("month", "c")
                else:
                    total = models.MoneyLog.objects.filter(Q(**data_dict), new_old=False, member_name__kefu=kefu
                                                       ).annotate(month=TruncMonth("success_time")).values("month").annotate(c=Sum('money')).values("month", "c")
               
            else:
                if kefu=='0':
                    total = models.MoneyLog.objects.filter(Q(**data_dict), 
                                                       ).annotate(month=TruncMonth("success_time")).values("month").annotate(c=Sum('money')).values("month", "c")
                else:
                    total = models.MoneyLog.objects.filter(Q(**data_dict), member_name__kefu=kefu
                                                       ).annotate(month=TruncMonth("success_time")).values("month").annotate(c=Sum('money')).values("month", "c")
        my_context = {
            'total_money': total,
        }
        return super(MoneyLogAdmin, self).changelist_view(request,
                                                          extra_context=my_context)

 

posted @ 2023-05-05 09:16  super_ip  阅读(129)  评论(0编辑  收藏  举报