TypeError用于在Django ORM中获取按月数据

时间:2019-12-28 08:49:56

标签: django orm typeerror

错误:

  

/ production / dashboard中的TypeError-报告不是在格式化字符串期间转换了所有参数

我不知道为什么会出现这种错误。它适用于包含,范围,gte,lte,但如果我写年或月,则会给我错误。

这是我的模特:

 class Production(models.Model):
    bcb = models.ForeignKey(BCBarcode, on_delete=models.CASCADE, null=True)
    process = models.ForeignKey(Process, on_delete=models.CASCADE, null=True)
    part = models.ForeignKey(Part, on_delete=models.CASCADE, null=True)
    quantity = models.IntegerField(default = 0)
    accepted_qty = models.IntegerField(default = 0)
    rejected_qty = models.IntegerField(default = 0)
    remarks = models.TextField(null=True)
    scan_user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
    scanned_at = models.DateTimeField(default=timezone.now)

这是我的代码:

def dashboardReport(request):
if not request.user.is_authenticated:
    return redirect('index')
else:
    if request.user.is_superuser:
        total_po = PO.objects.all().count()
    elif request.user.company_id == 0:
        total_po = PO.objects.all().count()
    else:
        total_po = PO.objects.filter(company=request.user.company).count()
        cutting_data = getProcessWiseTotalQuantity(company=request.user.company,process=1)
        sewing_data = getProcessWiseTotal_IOQuantity(company=request.user.company,process1=14,process2=15)
        printing_data = getProcessWiseTotal_IOQuantity(company=request.user.company,process1=4,process2=7)
        embroidery_data = getProcessWiseTotal_IOQuantity(company=request.user.company,process1=10,process2=13)

    buyers = Buyer.objects.all().count()
    context = {
        'buyers':buyers,
        'total_po':total_po,
        'cutting_details':cutting_data,
        'sewing_details':sewing_data,
        'printing_details':printing_data,
        'embroidery_details':embroidery_data,
    }
    return render(request,'report/dashboard_report.html',context)

 def getProcessWiseTotalQuantity(company,process):
    if company is None:
        today = datetime.datetime.today()
        print(today)
        last_month = datetime.datetime.today() - timedelta(days=30)
        todays_data = Production.objects.filter(process=process).count()
        return todays_data
    else:
        today = datetime.datetime.today().date()
        last_day = datetime.datetime.today() - timedelta(days=1)
        this_week = datetime.datetime.today() - timedelta(days=6)
        last_day_of_this_week = datetime.datetime.today() - timedelta(days=1)
        last_week = (datetime.datetime.today() - timedelta(days=13))
        print(today.month)
        last_month = datetime.datetime.today() - timedelta(days=30)
        text_format = ['Today','Last Day','This Week','Last Week','This Month','Last Month']
        result = []

        todays_data = Production.objects.filter(process=process,part=1,scanned_at__contains=today,bcb__bc__company=company).aggregate(total_todays_data=Sum('accepted_qty'))

        last_days_data = Production.objects.filter(process=process,part=1,scanned_at__contains=last_day,bcb__bc__company=company).aggregate(total_previous_days_data=Sum('accepted_qty'))

        this_weeks_data = Production.objects.filter(process=process,part=1,scanned_at__gte=this_week,bcb__bc__company=company).aggregate(total_this_weeks_data=Sum('accepted_qty'))

        last_weeks_data = Production.objects.filter(process=process,part=1,scanned_at__range=[last_week,last_day_of_this_week],bcb__bc__company=company).aggregate(total_last_weeks_data=Sum('accepted_qty'))

        this_months_data = Production.objects.filter(process=process,part=1,scanned_at__month__gte=today.month,bcb__bc__company=company).aggregate(total_last_weeks_data=Sum('accepted_qty'))


        print('This Weeks data:',last_weeks_data['total_last_weeks_data'])
        return todays_data['total_todays_data']

0 个答案:

没有答案