Google Maps in the Django admin panel

KatiaG
Jun 6, 2020

--

I would like to share with you a nice widget django-map-widgets which I have found some time ago.

World map

I use PostGis to save coordinates in my application. And I use Django Admin panel to add it to my Data Base. First I used admin.OSMGeoAdmin to show a world map. But it has some problems:

  • We use Google Map to show coordinates to our users. And sometimes we can see a little difference. It is not critical, but we do not like it.
  • It is just easier to look for a place by address on Google Map.

So I found a nice widget which can show Google Map in Django admin panel — django-map-widgets.

Installation:

pip install django-map-widgets

You will need an API key to get an access to Google Maps API. You can make it here https://developers.google.com/maps/documentation/javascript/get-api-key.

Add to settings.py:

INSTALLED_APPS = [
...
'mapwidgets',
]
...
# Map settings
GOOGLE_MAP_API_KEY = <key>
MAP_WIDGETS = {
"GooglePointFieldWidget": (
("zoom", 15),
("mapCenterLocationName", "amsterdam"),
("GooglePlaceAutocompleteOptions", {'componentRestrictions': {'country': 'nl'}}),
("markerFitZoom", 12),
),
"GOOGLE_MAP_API_KEY": GOOGLE_MAP_API_KEY
}

And admin.py:

from mapwidgets.widgets import GooglePointFieldInlineWidget
from .models import Place
from django.contrib.gis.db.models import GeometryField

class PlaceAdmin(admin.ModelAdmin):
...
formfield_overrides = {
GeometryField: {"widget": GooglePointFieldInlineWidget}
}

admin.site.register(Place, PlaceAdmin)

Done! You can use Google Maps in your Django admin panel.

If you enjoyed this post, you might also like “User Authentication with Django REST framework and Simple JWT”.

--

--

KatiaG

Software Developer, +12 years coding. Bachelors in CompSci.