Opened 4 years ago

Last modified 4 years ago

#27674 new Cleanup/optimization

Deprecate GeoModelAdmin and OSMGeoAdmin

Reported by: Claude Paroz Owned by: nobody
Component: GIS Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The geometry fields now have default map widgets, so the need of gis-specific ModelAdmin classes is therefore less relevant.

However, the fact that ModelAdmin.formfield_for_dbfield is not documented does not help, so I'd suggest to document it first.

Change History (3)

comment:1 Changed 4 years ago by Tim Graham

Triage Stage: UnreviewedAccepted

Could you describe the relevance of formfield_for_dbfield in a bit more detail?

comment:2 Changed 4 years ago by Claude Paroz

Sure. The typical example is to be sure all geometry widgets use a specific map widget, or specific map widget attributes.
With formfield_overrides, you'd have to specify all field types:

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.PointField: {'widget': MyCustomMapWidget},
        models.PolygonField: {'widget': MyCustomMapWidget},
        models.LineField: {'widget': MyCustomMapWidget},
        models.MultiPointField: {'widget': MyCustomMapWidget},

which is not nice if you have many geometry field types in your project. I think it's a case where formfield_for_dbfield is handy:

class MyModelAdmin(admin.ModelAdmin):
    def formfield_for_dbfield(self, db_field, request, **kwargs):
        if isinstance(db_field, models.GeometryField) and db_field.dim < 3:
            kwargs['widget'] = OSMWidget(default_lon=151, default_lat=-33)
            return db_field.formfield(**kwargs)
            return super(GeoModelAdmin, self).formfield_for_dbfield(db_field, request, **kwargs)

An alternative would be to still provide a GIS admin utility (subclass or mixin) which sets the same map widget for all geometry fields, like the current GeoModelAdmin.get_map_widget(). Basically the current code without all the boiler plate code copying class attributes to widget attributes.

comment:3 Changed 4 years ago by Tim Graham

Another idea: I like to see this sort of issue solved at the form level, if possible, to allow reusing the solution outside the admin.

Note: See TracTickets for help on using tickets.
Back to Top