Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disabling Openlayers map in GeoDjango Admin

I'm using Django 1.6 with Postgres/PostGIS (GeoDjango). I noticed when I upgraded from 1.5 to 1.6 that I am no longer able to add spatial data using WKT through the admin page. Previously, I could paste in the WKT of the geometry that I wanted to display into a text box. Now, when I go to the admin page, a map is displayed which does allow me to edit the geometry, but I cannot add data.

Is there a simple fix that allows me to disable showing this map?

like image 516
djq Avatar asked Dec 26 '22 13:12

djq


1 Answers

You can override the widget used in any admin form with formfield_overrides.

from https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_overrides

ModelAdmin.formfield_overrides

This provides a quick-and-dirty way to override some of the Field options for use in the admin. formfield_overrides is a dictionary mapping a field class to a dict of arguments to pass to the field at construction time.

So in your case, you'd want to override the lovely open layers map with a plain old text field. The following would replace the maps with a text input for any PointField in the GeoModel model.

from app.models import GeoModel
from django.forms.widgets import TextInput
from django.contrib.gis.db import models
from django.contrib import admin

class DirectAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.PointField: {'widget': TextInput }
    }

admin.site.register(GeoModel, DirectAdmin)

You might find a Textarea makes reading the WKT a lot easier, so change the second import to:

from django.forms.widgets import Textarea

And use that in the override instead of the TextInput:

models.PointField: {'widget': Textarea }
like image 86
Kip Avatar answered Jan 01 '23 16:01

Kip