I'm building an application on Django 1.5 that stores location data with PostGIS. For the prototype, the form to create a new location record requires that the user enter the latitude and longitude coordinates (I assumed this would be the easiest setup to code).
I've created a ModelForm
like so:
class LocationForm(forms.ModelForm):
# Custom fields to accept lat/long coords from user.
latitude = forms.FloatField(min_value=-90, max_value=90)
longitude = forms.FloatField(min_value=-180, max_value=180)
class Meta:
model = Location
fields = ("name", "latitude", "longitude", "comments",)
So far, so good. However, the Location
model doesn't have latitude
nor longitude
fields. Instead, it uses a PointField
to store the location's coordinates:
from django.contrib.gis.db import models
class Location(models.Model):
name = models.CharField(max_length=200)
comments = models.TextField(null=True)
# Store location coordinates
coords = models.PointField(srid=4326)
objects = models.GeoManager()
What I'm looking for is where to inject the code that will take values for the latitude
and longitude
inputs and store them as a Point
object in the Location
's coords
field once the user submits the form with valid values.
E.g., I'm looking for the Django equivalent of Symfony 1.4's sfFormDoctrine::doUpdateObject()
method.