Follow-up: How to override the Map Widget in Django Admin in which Geom Data Loaded via LayerMapping?

This question follows up on my initial query about dynamically changing the color of shapefile layers on a Django Admin Leaflet map.

In my GeoDjango project, I'm loading geometry data via the LayerMapping method. Below is the load.py file I'm using for this:

from django.contrib.gis.utils import LayerMapping
from .models import Municipality

municipality_mapping = {
    'reg_code': 'Reg_Code',
    'reg_name': 'Reg_Name',
    'pro_name': 'Pro_Name',
    'mun_code': 'Mun_Code',
    'mun_name': 'Mun_Name',
    'mean_ctrl': 'mean_ctrl',
    'geom': 'MULTIPOLYGON',
}

zonalmeanctrl_shp = "path/to/shapefile.shp" 

def run(verbose=True):
    lm = LayerMapping(Municipality, zonalmeanctrl_shp, municipality_mapping, transform=False) 
    lm.save(strict=True, verbose=verbose)

In admin.py, I configured the admin panel with LeafletGeoAdmin:

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

class MunicipalityAdmin(LeafletGeoAdmin):
    list_display = ('pro_name', 'reg_code', 'reg_name', 'mun_code', 'mun_name', 'mean_ctrl')

admin.site.register(Municipality, MunicipalityAdmin)

Now, I would like to go further by customizing the map widget to dynamically style the geometries based on certain attributes (e.g., changing the color based on the mean_ctrl value). How can I override the map widget in Django Admin to apply dynamic colors to these shapefiles loaded via LayerMapping?

Вернуться на верх