问题描述
我想知道如何在管理员中更改默认的PointField可视化(Openstreetmap),以便我可以输入简单的纬度/经度,而不是在地图上选择一个点?
我查看了这个,但不能得到它在任何方式在Django 1.6b4
谢谢
如Bibhas所说,您可以覆盖该字段使用的小部件,但简单的文本输入可能不是有用的。所以这里是一个可以用于geodjango的PointField的完整小部件的例子:
class LatLongWidget(forms.MultiWidget):
将点输入分割为纬度/经度文本输入的小部件
def __init __(self,attrs = None,date_format =没有,time_format =无):
widgets =(forms.TextInput(attrs = attrs),
forms.TextInput(attrs = attrs))
super(LatLongWidget,self).__ init __(widgets ,attrs)
def解压缩(self,value):
如果值:
返回tuple(value.coords)
返回(无,无)
def value_from_datadict(self,data,files,name):
mylat = data [name +'_0']
mylong = data [name +'_1']
try:
point = Point(float(mylat),float(mylong))
除了ValueError:
return''
return point
现在您可以覆盖您的模型Admin:
from django.contrib.gis.db import models as geomodels
pre>
class CompanyAdmin(admin.ModelAdmin):
list_display =('name','approval','company_view')
list_filter =('approval',)
formfield_overrides = {
geomodels.PointField:{'widget':LatLongWidget},
}
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
解决方案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}, }
这篇关于GeoDjango PointField管理员可视化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!