- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 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/
我的一个模型中有一个FloatField。我使用ModelForms添加/编辑它。我需要的是 当我编辑时,存储在FloatField中的Float值将以2个精确点进行渲染。 即如果我的存储值为1000
考虑以下示例模型: class Product(models.Model): weight = FloatField(db_index=True, null=True, blank=True)
我在我的 Django 模型中存储了一些 float 据,只有特定范围的值才有意义。因此,我想在模型和 SQL 约束级别都施加这些限制。 例如,我想做这样的事情: class Foo(Model):
*mit = 13311 std::istringstream iss(*mit); double temp; iss.setf(ios::fixed, ios::floatfield); iss.p
我的模型中有一个 FloatField。该区域右侧的增加和减少按钮通常增加或减少 1。是否有可能做到这一点 1000 x 1000?我该怎么做? 模型.py class Customer(models
我正在尝试创建一个自定义表单字段,其工作方式与浮点字段的所有意图和目的相同,但(默认情况下)输出不带尾随零的浮点值,例如33 而不是 33.0 我尝试像这样简单地扩展 django.forms.Flo
我有一个 FloatField,它可以有 22.33405、33.567 等值。现在,我需要查询最多使用小数点后 2 位,如下所示 #find all objects with value equal
java fx.h 中有 FloatField 这样的东西吗?我需要能够编写和编辑 float 。所以我用字符串测试了它。但setCellProperty不支持 float TableColumn c
我很好奇哪个更适合作为货币字段?我会做一些简单的操作,比如取差,新旧价格之间的百分比。我计划在零后保留两位数字(即 10.50),如果这些数字为零,大多数情况下,我将隐藏这些数字并将其显示为“10”
当我尝试获取“高度”和“体重”变量的值以用于计算时,我在第 8 行收到错误: “类 FloatField”未定义“truediv”,因此不能在其实例上使用“/”运算符。此检查检测到应该解析但没有解
我有一个现有的填充数据库,我想将 PositiveIntegerField 转换为 FloatField。我正在考虑简单地进行迁移: migrations.AlterField(
我试图将无穷大放入 FloatField 中,但这似乎不起作用。我该如何解决这个问题? f = DjangoModel(float_value=float('inf')) #ok f.save() #
我有一个类,我正在尝试向它添加一个新的 FloatField。 Django 想要一个默认值来填充现有行(合理)。但是,它拒绝接受我给它的任何值。 You are trying to add a no
我正在寻找一种将 Django 中的 FloatField 限制为小数点后两位的方法,有没有人知道如何在不使用 DecimalField 的情况下完成此操作。 我尝试了 decimal_places=
我希望能够查询一个模型以获取对于特定浮点字段没有值的记录。问题是基本查询: Model.objects.filter(float_field__is_null=True) 返回以下错误:django
我有一个带有价格字段的 Django 模型: class Book(models.Model): title = models.CharField(max_length=200) pr
我是 Python 和 Django 新手。 我希望我的模型具有经过范围验证的 float 。来自 this answer我写的是: class FloatRangeField (FloatField
问题 我需要添加将 minimum_commission 保存为正数的功能,但 None 也可用。我已经添加了序列化器: class Appartement(models.Model): mi
如何在 Django 模型中设置 FloatField 可以容纳的最大值? price_upper_bound = models.FloatField(null=True, blank=True, d
我正在使用 Django pgcrypto fields加密模型中的金额值 Invoice如下: from pgcrypto import fields class Invoice(models.Mo
我是一名优秀的程序员,十分优秀!