144đź‘Ť
Float is generally an approximation, see here for some simple examples. You could get very nice results modifying your model to something like DecimalField(max_digits=9, decimal_places=6)
, since decimals are very important in coordinates but using more than 6 is basically meaningless.
43đź‘Ť
Use PointField to store lat long
p = Point(85.3240, 27.7172,srid=4326)
Django: 1.X:
https://docs.djangoproject.com/en/1.11/ref/contrib/gis/model-api/#pointfield
Django: 2.X:
https://docs.djangoproject.com/en/2.2/ref/contrib/gis/model-api/#pointfield
Django: 3.X:
https://docs.djangoproject.com/en/3.0/ref/contrib/gis/model-api/#pointfield
- [Django]-Django dump data for a single model?
- [Django]-Django: Calculate the Sum of the column values through query
- [Django]-Django can' t load Module 'debug_toolbar': No module named 'debug_toolbar'
9đź‘Ť
The suggestion here to use DecimalField(max_digits=9, decimal_places=6)
resulted in errors for me when trying to save locations from Google Maps.
If we assume that the most common use case is retrieving point locations from the Maps API, and a typical URL from Google Maps when right-clicking and selecting “What’s Here?” yields something like:
https://www.google.com/maps/place/37°48'52.3"N+122°17'09.1"W/@37.814532,-122.2880467,17z
(random place)
So the longitude has 15 places before and after the decimal, which is why the accepted answer doesn’t work. Since there’s no reason to validate for “as short as possible” and db storage is cheap, I’m using:
max_digits=22,
decimal_places=16)
- [Django]-Can you give a Django app a verbose name for use throughout the admin?
- [Django]-Django: FloatField or DecimalField for Currency?
- [Django]-STATIC_ROOT vs STATIC_URL in Django