How to tweak GeoDjango Admin

In my GeoDjango tutorial series, I shared details on the development of a web-mapping solution using GeoDjango. Depending on the structure of the application and the use, Django admin is a very useful feature in this framework. There is power in the admin.

In this post, I will share some tips on how to tweak your GeoDjango admin to enhance display, look and feel and functionality. By default, Django comes with OSMGeoAdmin which is an extension used with spatial models. We want to replace this option with other widgets and tools that are even better.

GeoDjango Admin

Django Leaflet

For us to use Leaflet in our project admin, we need to install it. To install Leaflet, run

pip install django-leaflet    #Remember to add 'leaflet' to INSTALLED_APPS

This will install the latest version of Leaflet in your Django project. Once installed, edit your to the following(Depending on your models)

from django.contrib import admin
from .models import MyModel
from leaflet.admin import LeafletGeoAdmin

# Register your models here.
class MyModelAdmin(LeafletGeoAdmin):
    list_display = ('name','date_reported','location')
    search_fields = ('name',)
    filter_fields = ('name','date_reported')

In this scenario, we use Leaflet for the admin section(spatial models) in the project. LeafletGeoAdmin will output an interface as the one shown below;

GeoDjango Admin

Spot the difference?

The LeafletGeoAdmin looks way much better, sleek and has more tools than OSMGeoAdmin.

One more thing;

Try adding a new record of the Incidence model from the admin. You will not that the geo field is zoomed to [0,0] by default and displays multiple worlds(If there is something like this). It also doesn’t limit the zoom levels on the map.

Let’s work on this. To customize this bit, let’s add a configuration in our file. Add this code to the bottom;

    'DEFAULT_CENTER': (-.023, 36.87),
    'DEFAULT_ZOOM': 5,
    'MAX_ZOOM': 20,
    'SCALE': 'both',
    'ATTRIBUTION_PREFIX': 'Inspired by Life in GIS'

Django Map Widgets

As earlier indicated, we can make our Django admin even more beautiful and flexible using a number of tools. Map widgets is one of these tools.

Currently, only Google Maps services are supported. I look forward to the support of other services that exist out there.

To install Django Map Widgets, run the command;

pip install django-map-widgets #Remember to add 'map_widgets' to INSTALLED_APPS

Adjust your file as below;

from django.contrib.gis.db import models
from mapwidgets.widgets import GooglePointFieldWidget

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.PointField: {"widget": GooglePointFieldWidget}

Note we have the class has admin.ModelAdmin. Google Map Widgets is a good option if you don’t want to change the base class in your admin declarations.

For this widget, remember to collectstatic so as to fetch the static files into your project. To collect static, run

python collectstatic

Refresh your admin and check the changes

Using this widget, it offers options to enter lat and long, using your location, dragging a marker to your point of interest and searching for an address of the point of interest.

You can try to workout a model with other types of data such as polylines and areas.

Well, I think we’ve done much tweaking on our GeoDjango admin.

What other methods do you use to customize your GeoDjango admin? Share with us.

Keep posted for more posts.

Leave a Reply

Your email address will not be published. Required fields are marked *

two × one =

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Wanjohi Kibui

A GIS user with interests in web and desktop systems development, FOSS advocate, trainer and mentor. Lives in Nairobi but finds adventure in travelling. The "Life in GIS" phrase is as a result of the many told and untold stories in the Geospatial realm.

More Reading

Post navigation