4

I've been redesigning my application to allow users to set markers more easily. One thing I need to include for backwards compatibility purposes is that the new model must have a latitude and longitude field in the database.

Is it possible to modify the GeoDjango admin so that the latitude/longitude fields automatically populate after I select a point on the map?

Here's a screenshot of how my admin is currently: admin image

4

I ended up solving this by overriding the admin model in admin.py, and setting the latitude/longitude fields as read-only like this:

class MarkerAdmin(admin.OSMGeoAdmin):
    default_lon = -93
    default_lat = 27
    default_zoom = 15
    readonly_fields = ('Latitude','Longitude')
admin.site.register(Marker, MarkerAdmin) 

I then modified the relevant model in my models.py to override the save function so that it assigned the longitude and latitude fields from the PointField.

class Marker(models.Model):
    MarkerID= models.AutoField(primary_key=True) 
    Name= models.CharField(max_length=40)
    mpoint = models.PointField()
    objects = models.GeoManager()
    Latitude = models.DecimalField(max_digits=10, decimal_places=6)
    Longitude = models.DecimalField(max_digits=10, decimal_places=6)

    def save(self, *args, **kwargs):
        self.Latitude  = self.mpoint.y
        self.Longitude = self.mpoint.x   
        super(Marker, self).save(*args, **kwargs)  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.