Search code examples
djangodjango-templatesadminchangelist

Get data displayed in a django admin change_list


I've got an admin change_list with a list of companies and thier addresses. I want to plot this onto a google map. I've got google maps coming up, and the right data displaying in the change_list, and i've copied out the change_lsit template so Im overriding it.

They problem is that I need to get the data from the page, which I assume is in the change_list object and put that into the javascript so I can plot the addresses onto the map.

But I havent been able to find any doco on the change_list object or how to get at the data displayed. Normally you can just do something like {% model.attribute %} and if I was able to do something like that and wrap it in a for loop that would be great.

So any idea about how to get at the data in a change_list?

Cheers

Mark


Solution

  • There are at least two approaches. One is to use an api call from javascript to get the data. The other, which is what you are planning I think, is to pass the data from change_list into your template. Here is an example (may be out of date for googlemaps, but will give you the idea). It assume change_list has two fields lat and longt:

    <div id="map" style="width: 100%; height: 300px;"></div>
    <script type="text/javascript" charset="utf-8">
        if (GBrowserIsCompatible()) {
            var map = new GMap2(document.getElementById("map"));
            map.setUIToDefault();
    
            {% for location in change_list %} 
                var point = new GLatLng({{ location.lat|default:"0.0" }}, {{ location.longt|default:"0.0" }});
                map.addOverlay(new GMarker(point)); 
            {% endfor %}
      }
    </script>