Python,Django float()参数必须是字符串或数字,而不是';非类型

Python,Django float()参数必须是字符串或数字,而不是';非类型,python,django,web,Python,Django,Web,我在运行服务器时出错: “float()参数必须是字符串或数字,而不是'NoneType' 该网站运行良好,但在我删除数据库后,出现了此错误 views.py def home(request): today = datetime.date.today() tomorrow = today + datetime.timedelta(days=1) service_check = Service.objects.filter( date_of_tre

我在运行服务器时出错: “float()参数必须是字符串或数字,而不是'NoneType'

该网站运行良好,但在我删除数据库后,出现了此错误

views.py    
def home(request):
    today = datetime.date.today()
    tomorrow = today + datetime.timedelta(days=1)
    service_check = Service.objects.filter(
        date_of_treatment__gte=today, date_of_treatment__lt=tomorrow)
    total_service_today = service_check.count()

    total_price = service_check.aggregate(Sum('price'))
    total_price_today = float(total_price['price__sum'])

    context = {'service_check': service_check,
               'total_price_today': total_price_today}
    return render(request, 'main/dashboard.html', context)


models.py
class Service(models.Model):
    patient = models.ForeignKey(
        Patient, related_name='patient', null=True, on_delete=models.SET_NULL)
    attending_doctor = models.ForeignKey(
        Doctor, related_name='attending_doctor', null=True, on_delete=models.SET_NULL)
    treatment = models.ForeignKey(
        Treatment, related_name="treatment", on_delete=models.SET_NULL, null=True)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    date_of_treatment = models.DateTimeField()

处理此问题的一种方法(返回零而不是
None
)是将行
total\u price=service\u check.aggregate(Sum('price'))
替换为:

from django.db.functions import Coalesce

total_price = service_check.aggregate(Coalesce(Sum('price'), 0))

如果你想了解更多关于凝聚的信息,你可以找到它。

总价['price\uu sum']
返回一个
谢谢你的消息,我是import(来自django.db.models.functions import Coalesce),并更改总价。在此之后,我出现了一个错误:复杂聚合需要一个别名i修复,聚合方法需要一个名称:total_price=service_check.aggregate(sum=(Coalesce(sum('price'),0))); 今天的总价=总价['sum']