already_transferred = Transfer.objects.all().filter(transferred_by=request.user, account_id=id).aggregate(total=Coalesce(Sum('amount'), 0))
服务器给出了这个错误
function sum(text) does not exist
LINE 1: SELECT COALESCE(SUM("API_insidetransfer"."amount"), 0) AS "a...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
这是我的模型
class Transfer(models.Model):
transferred_by = models.CharField(max_length=120)
account_id = models.CharField(max_length=120)
amount = models.CharField(max_length=120, default=0)
created_on = models.DateTimeField(auto_now=True)
我该如何解决这个问题?
最佳答案
问题是您的amount
是一个CharField
。你不能对charfields求和。例如,'foo'
和 'bar'
的总和是多少。
因此,您应该在模型级别解决问题:将 amount
设为 DecimalField
(或 IntegerField
)。例如:
class Transfer(models.Model):
transferred_by = models.CharField(max_length=120)
account_id = models.CharField(max_length=120)
amount = <b>modelsDecimalField(max_digits=12, decimal_places=2, default=0)</b>
created_on = models.DateTimeField(auto_now=True)
关于python - 如何解决没有函数与给定名称和参数类型匹配的问题。您可能需要添加显式类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58238988/