Philipp Bosch

Building the Web since 1995

April 04, 2011

django-geoposition – a tiny app to store geopositions in Django models

Yesterday I was looking for a simple way to add a geoposition (latitude and longitude) field to a Django model without the whole GeoDjango aka django.contrib.gis hassle but unfortunately was not successful. The approaches I found were either too minimal, overloaded or outdated. So I came up with a simple solution myself.

May I present to you: django-geoposition.

Installation

Say you want to store a geoposition in one of your models. It’s three simple steps:

Step #1
Add geoposition to your INSTALLED_APPS.

Step #2
Add a GeopositionField to one of your models.

from django.db import models
from geoposition.fields import GeopositionField

class PointOfInterest(models.Model):
    name = models.CharField(max_length=100)
    position = GeopositionField()

Step #3
There is no step #3.

Usage

Also very simple:

>>> from myapp.models import PointOfInterest
>>> poi = PointOfInterest.objects.get(id=1)
>>> poi.position
Geoposition(52.522906,13.41156)
>>> poi.position.latitude
52.522906
>>> poi.position.longitude
13.41156

Widget

If used in a form (e.g. in the admin) the field is displayed together with a Google Maps widget looking like this:

Screenshot

… and that’s basically it.

The source is on GitHub and PyPI. Basic documentation is available on Read the docs.