Commit a4756fd8 authored by Nicolas Noé's avatar Nicolas Noé
Browse files

Admin: Custom lat/lon widget instead of OL map.

parent 98ffa2f0
from django.contrib import admin
from specimens.models import Specimen
from django import forms
from .models import Specimen
from .widgets import LatLongWidget
# Custom form to provide lat/lon widget instead of OL map.
class MyAdminForm(forms.ModelForm):
class Meta:
model = Specimen
fields = "__all__"
widgets = {
'location': LatLongWidget
}
class SpecimenAdmin(admin.ModelAdmin):
pass
form = MyAdminForm
admin.site.register(Specimen, SpecimenAdmin)
<p>
{{ lat_label }} {% with widget=widget.subwidgets.1 %}{% include widget.template_name %}{% endwith %}
{{ long_label }} {% with widget=widget.subwidgets.0 %}{% include widget.template_name %}{% endwith %}
</p>
\ No newline at end of file
from django.forms import widgets
from django.contrib.gis.geos import Point
class LatLongWidget(widgets.MultiWidget):
"""
A Widget that splits Point input into latitude/longitude text inputs.
"""
template_name = 'django/forms/widgets/latlong.html'
def __init__(self, attrs=None):
_widgets = (
widgets.TextInput(attrs=attrs),
widgets.TextInput(attrs=attrs),
)
super(LatLongWidget, self).__init__(_widgets, attrs)
def get_context(self, name, value, attrs):
context = super(LatLongWidget, self).get_context(name, value, attrs)
context['lat_label'] = "Lat:"
context['long_label'] = "Lon:"
return context
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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment