[Django]-Geodjango view admin list view on a map

0👍

Probably it is not the best solution, I haven’t found a way that the admin would do it for me, so I did it manually, this is what I’ve done:

Override the changelist view in order to inject the data you want to display in the admin view, something like:

class MyAdmin(admin.ModelAdmin):

    def changelist_view(self, request, extra_context=None):
        extra_context = extra_context or {}
        extra_context['mypoints'] = MyPoint.objects.all()
        return super(MyAdmin,
                     self).changelist_view(request,
                                           extra_context=extra_context)

Then, create the template in /templates/admin/my_app/change_list.html, that overrides and extend the base change_list.html, adding a typical google map:

{% extends "admin/change_list.html" %} 
{% block content %}
{% load staticfiles %}
<script src="https://maps.googleapis.com/maps/api/js"></script>

<script>
function initialize() {
    //Typical map initialization here

    {% for point in mypoints %}
        var c = new google.maps.Circle({
            //circle config
            map: map,
            center: {lat: {{point.0 |stringformat:".3f" }}, lng: {{point.1|stringformat:".3f" }}},
            radius: 0.1
        });
    {% endfor %}
}
</script>

{{block.super}} {% endblock %}

Note the {{block.super}} that maintains the rest of the functionality of the block. Also, I have had some troubles with the lat-long, and had to change point.1 with point.0 to achieve a correct representation, don’t know yet if it is a different convention between the default systems of geodjango and google maps.

👤drodri

Leave a comment