gpt4 book ai didi

python - python/Django 中的十进制数

转载 作者:行者123 更新时间:2023-11-28 16:37:07 24 4
gpt4 key购买 nike

我正在尝试在数据库中执行插入操作,但它似乎是小数位的问题。

我有 latitudelongitude,如下所示:

latitude = models.DecimalField(max_digits=30, decimal_places=15)

然后,当我尝试插入时,我写:

Place(name="center", description="study center", latitude=41.214555, longitude=2.121451)

但是我得到了那个错误:“量化结果对于当前上下文有太多的数字”

我应该可以得到这些数字,对吗?我不明白为什么会出现该错误。如果有人可以帮助我,我将非常感激。

最佳答案

考虑这个模型:

class Place(models.Model):
name = models.TextField()
description = models.TextField()
longitude = models.DecimalField(max_digits=30, decimal_places=15)
latitude = models.DecimalField(max_digits=30, decimal_places=15)

这应该有效(允许使用的最大位数和小数位数):

p = Place(
name='Name',
description='Description',
latitude=0123456789012345.0123456789012345,
longitude=0123456789012345.0123456789012345
)
p.save()

这应该会失败(顶部的值):

p = Place(
name='Name',
description='Description',
latitude=01234567890123456.01234567890123456,
longitude=01234567890123456.01234567890123456
)
p.save()

确保在更改 Django 代码后相应地迁移/更改了数据库。

关于python - python/Django 中的十进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24524690/

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