在django ORM中获取逐月数据的TypeError

在django ORM中获取逐月数据的TypeError,django,orm,typeerror,Django,Orm,Typeerror,错误: /production/dashboard报告中的TypeError在字符串格式化期间未转换所有参数 我不知道我为什么会犯这种错误。它适用于contains、range、gte、lte,但如果我每年或每月写信,它会给我错误 这是我的模型: class Production(models.Model): bcb = models.ForeignKey(BCBarcode, on_delete=models.CASCADE, null=True) process = mod

错误:

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

我不知道我为什么会犯这种错误。它适用于contains、range、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']

你的queryset看起来不错。错误似乎在其他地方。你能分享你使用这个月数据的代码吗?如果我使用扫描的月=今天。月或扫描的月=今天。月@engin_Ipekw这行“打印('this Weeks data:',last Weeks_data['total_last Weeks_data')”打印会出现错误。我测试了你查询集的所有变化,它们对我来说都很好。错误发生在我假设的使用getProcessWiseTotalQuantity方法的部分。从getProcessWiseTotalQuantity中,我得到一些要在模板中呈现的数据。。它只是给错误,如果我用了一个月,否则它可以查看模板上的数据。