Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GeoDjango PointField admin visualization

I was wondering how I could change the default PointField visualization (the Openstreetmap) in admin so that I could enter simple latitude/longitude instead of select a point on the map?

I looked at this one Latitude/longitude widget for pointfield? but could not get it working in any way in Django 1.6b4

Thanks

like image 564
Rok Avatar asked Oct 07 '13 17:10

Rok


2 Answers

As Bibhas says you can override the widget used for the field, but the simple text input may not be usefull. So here is an example with a full widget that can be used for PointField of geodjango:

class LatLongWidget(forms.MultiWidget):
    """
    A Widget that splits Point input into latitude/longitude text inputs.
    """

    def __init__(self, attrs=None, date_format=None, time_format=None):
        widgets = (forms.TextInput(attrs=attrs),
                   forms.TextInput(attrs=attrs))
        super(LatLongWidget, self).__init__(widgets, attrs)

    def decompress(self, value):
        if value:
            return tuple(value.coords)
        return (None, None)

    def value_from_datadict(self, data, files, name):
        mylat = data[name + '_0']
        mylong = data[name + '_1']

        try:
            point = Point(float(mylat), float(mylong))
        except ValueError:
            return ''

        return point

And now you can override your model Admin:

from django.contrib.gis.db import models as geomodels
class CompanyAdmin(admin.ModelAdmin):
    list_display = ('name', 'approval', 'company_view',)
    list_filter = ('approval',)
    formfield_overrides = {
        geomodels.PointField: {'widget': LatLongWidget},
    }
like image 99
César Avatar answered Nov 07 '22 22:11

César


You can override a widget with another in Django admin. From the documentation -

from django.db import models
from django.contrib import admin

# Import our custom widget and our model from where they're defined
from myapp.widgets import RichTextEditorWidget
from myapp.models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    formfield_overrides = {
        models.TextField: {'widget': RichTextEditorWidget},
    }

This overrides TextField with RichTextEditorWidget. Just find the field type for point field and override it with TextField.

like image 2
Bibhas Debnath Avatar answered Nov 07 '22 22:11

Bibhas Debnath