Django 十进制字段“。”代替 ”,”
Django DecimalField "." instead of ","
我认为这似乎是千位分隔符的语言问题。如果我在 settings.py 中使用 en-us 它可以工作,但是如果我将其更改为丹麦语 'da' 它会将其转换为逗号...我可以使用 'da' 并强制使用模型吗英语?
如果我将 11.766647100448608
粘贴或键入到管理员的 lon 字段中并点击保存,它会将 .
更改为 ,
,如 11,766647100448608
。另一个字段 lat 的情况也是如此。这是正常行为吗,这是我的问题吗?
我需要句点,因为它是 mapbox 的坐标。而我的标记最终在地球的另一边离开了。
来自模特:
class Map(models.Model):
project = models.ForeignKey(Project, related_name='map')
lon = models.DecimalField(max_digits=17, decimal_places=15, default='')
lat = models.DecimalField(max_digits=17, decimal_places=15, default='')
来自模板:
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
{{ map.lon }},
{{ map.lat }}
]
}
这就是我想要的:
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
11.766647100448608,
55.22922803094453
]
}
}
但这就是我得到的:
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
11,766647100448608,
55,22922803094453
]
}
}
django 管理员的行为可能是正确的;问题在于如何将十进制字段转换为模板中的字符串。这可能是由于国际化。事实上,DECIMAL_SEPARATOR 设置存在,但 "the locale-dictated format has higher precedence".
我建议您在模板中尝试以下内容:
{% load l10n %}
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
{{ map.lon|unlocalize }},
{{ map.lat|unlocalize }}
]
}
unlocalize
模板标记在 Django documentation 中进行了描述。
我认为这似乎是千位分隔符的语言问题。如果我在 settings.py 中使用 en-us 它可以工作,但是如果我将其更改为丹麦语 'da' 它会将其转换为逗号...我可以使用 'da' 并强制使用模型吗英语?
如果我将 11.766647100448608
粘贴或键入到管理员的 lon 字段中并点击保存,它会将 .
更改为 ,
,如 11,766647100448608
。另一个字段 lat 的情况也是如此。这是正常行为吗,这是我的问题吗?
我需要句点,因为它是 mapbox 的坐标。而我的标记最终在地球的另一边离开了。
来自模特:
class Map(models.Model):
project = models.ForeignKey(Project, related_name='map')
lon = models.DecimalField(max_digits=17, decimal_places=15, default='')
lat = models.DecimalField(max_digits=17, decimal_places=15, default='')
来自模板:
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
{{ map.lon }},
{{ map.lat }}
]
}
这就是我想要的:
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
11.766647100448608,
55.22922803094453
]
}
}
但这就是我得到的:
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
11,766647100448608,
55,22922803094453
]
}
}
django 管理员的行为可能是正确的;问题在于如何将十进制字段转换为模板中的字符串。这可能是由于国际化。事实上,DECIMAL_SEPARATOR 设置存在,但 "the locale-dictated format has higher precedence".
我建议您在模板中尝试以下内容:
{% load l10n %}
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [
{{ map.lon|unlocalize }},
{{ map.lat|unlocalize }}
]
}
unlocalize
模板标记在 Django documentation 中进行了描述。