gpt4 book ai didi

Django - 注释加密的 TextField 并将其转换为 FloatField 到小数点后两位

转载 作者:行者123 更新时间:2023-11-29 13:13:05 27 4
gpt4 key购买 nike

我正在使用 Django pgcrypto fields加密模型中的金额值 Invoice如下:

from pgcrypto import fields

class Invoice(models.Model):
# Some other fields
amount_usd = fields.TextPGPSymmetricKeyField(default='')

objects = InvoicePGPManager() # Manager used for PGP Fields

我正在使用 TextPGPSymmetricKeyField因为我必须将该值存储为 float 和 django-pgcrypto-fields没有 FloatField 的等效项。

现在我需要传递这个 amount_usd通过 API 的值,我必须将小数限制为最多两位。

我试过使用以下方法:

Invoice.objects.all().values('amount_usd').annotate(
amount_to_float=Cast('amount_usd', FloatField())
)

但这会产生错误,因为无法将字节(加密数据)转换为 float 。

我也试过用这个:

from django.db.models import Func

class Round(Func):
function = 'ROUND'
template='%(function)s(%(expressions)s, 2)'


Invoice.objects.all().annotate(PGPSymmetricKeyAggregate(
'amount_usd')).annotate(amount=Cast(
'amount_usd__decrypted', FloatField())).annotate(
amount_final = Round('amount'))

我收到以下错误:

django.db.utils.ProgrammingError: function round(double precision, integer) does not exist
LINE 1: ...sd, 'ultrasecret')::double precision AS "amount", ROUND(pgp_...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

有什么方法可以将加密字段转换为最多 2 位小数的 FloatField?

最佳答案

你的错误是:double precision AS "amount"那是因为您正在将 amount_usd 转换为 FloatField,它在 SQL 中转换为 double 。

尝试使用 DecimalField(在 SQL 中转换为数字类型)及其参数。

Invoices.objects.all().annotate(amount=Cast(
PGPSymmetricKeyAggregate('amount_usd'),
DecimalField(max_digits=20, decimal_places=2)))

在此处查看文档:Django DecimalField

关于Django - 注释加密的 TextField 并将其转换为 FloatField 到小数点后两位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52403156/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com