Displaying map in admin to add a point and record lat and lon

I’m working with a legacy database. The models include to fields, lat and lon, that describe the location of my objects. Not all objects have a lat or a lon and the information is not mandatory (blank=True).
In the admin, I’d like to display a small OSM based map in the form, with the possibility for the user to add a point if needed. This point would automatically fill in the lat and lon fields.
Is it possible?
I’ve already declared django.contrib.gis in INSTALLED_APPS, turned my PgSQL base into a PostGIS one and declared postgis and database engine in settings.py.
Thanks for your help!

Surely doable. Maybe this stackoverflow post may give you hints on how to proceed, even if it does not contains a GIS part

python - Django: Faking a field in the admin interface? - Stack Overflow.

Thanks for the link!

Luckily I found this tutorial that cover exactly my need: Adding GeoDjango to an existing Django project. The trick is to create a new PointField column and to populate it with a custom save() method, described in the tutorial.

Of course, if you are ready to alter the database and add a real point field to your table, that’s another story (and finally easier). But as you mentioned a legacy database, I provided a link to a possible solution without touching the existing database.

1 Like