user6285978
user6285978

Reputation:

Django DecimalField "." instead of ","

It seems it's a language problem with thousands separator, I think. If I use en-us in settings.py it works, but if I change it to danish 'da' it's converts it to a comma... Can I use 'da' and force a model to use english?

If I paste or type 11.766647100448608 into the lon field in the admin and hit save, it changes the . to a , like 11,766647100448608. The same is the case with the other field lat. Is that normal behavior, is it something on my end?

And I need the period because it is coordinates for mapbox. And my markers end up on way off on the other side of the globe.

From the model:

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='')

From the template:

"type": "Feature",
        "geometry": {
          "type": "Point",
          "coordinates": [
            {{ map.lon }},
            {{ map.lat }}
          ]
        }

THis is what I want:

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Point",
    "coordinates": [
      11.766647100448608,
      55.22922803094453
    ]
  }
}

But this is what I get:

{
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Point",
    "coordinates": [
      11,766647100448608,
      55,22922803094453
    ]
  }
}

Upvotes: 1

Views: 1585

Answers (1)

Régis B.
Régis B.

Reputation: 10588

The django admin is probably behaving correctly; the problem lies with how the decimal fields are converted to strings in the template. This is probably due to internationalization. Indeed, the DECIMAL_SEPARATOR setting exists but "the locale-dictated format has higher precedence".

I suggest you try the following in the template:

{% load l10n %}

"type": "Feature",
    "geometry": {
      "type": "Point",
      "coordinates": [
        {{ map.lon|unlocalize }},
        {{ map.lat|unlocalize }}
      ]
    }

The unlocalize template tag is described in the Django documentation.

Upvotes: 2

Related Questions