Geodjango管理员,不以地图显示点场 [英] Geodjango admin, display pointfield not as map

查看:95
本文介绍了Geodjango管理员,不以地图显示点场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但我找不到明确的答案.

This may be a stupid question but I can't find any clear answers.

如何更改Django Admin中的显示,以使Pointfield不会像OpenLayer Map那样显示,而是作为常规输入字段显示.我需要看很长的调试时间..

How do I change the display in the Django Admin so the Pointfield does not show up like a OpenLayer Map but as a regular input field. I need to see the long, lat for debugging..

我必须更改字段类型吗?小部件?

Do i have to change the field type? Widgets?

谢谢!

推荐答案

更新

这是我最后设法设法保留纬度和经度的单独字段而不必将其保存在数据库中的方式,因为这些值已经保存在PointField中.

This is how I managed -at last- to keep separate fields for lattitude and longitude without having to save them in the database since the values are already saved in the PointField.

这个想法是:

  • 如果要插入新条目,则纬度和经度字段将用于设置PointField
  • 如果我们打开现有的PointField条目,它将用于在相关FormField中提供纬度和经度值.

models.py

models.py

from django.contrib.gis.db import models as geomodels


class Entry(geomodels.Model):
    point = geomodels.PointField(
        srid=4326,
        blank=True,
        )

admin.py

from myapp.forms import EntryForm
from django.contrib import admin


class EntryAdmin(admin.ModelAdmin):
    form = EntryForm


admin.site.register(Entry, EntryAdmin)

forms.py

from django import forms
from myapp.models import Entry
from django.contrib.gis.geos import Point


class MarketEntryForm(forms.ModelForm):

    latitude = forms.DecimalField(
        min_value=-90,
        max_value=90,
        required=True,
    )
    longitude = forms.DecimalField(
        min_value=-180,
        max_value=180,
        required=True,
    )

    class Meta(object):
        model = MarketEntry
        exclude = []
        widgets = {'point': forms.HiddenInput()}

    def clean(self):
        super().clean()
        if any(x for x in ['latitude', 'longitude'] if x in self.changed_data):
            latitude = float(self.cleaned_data['latitude'])
            longitude = float(self.cleaned_data['longitude'])
            self.cleaned_data['point'] = Point(longitude, latitude)

    def __init__(self, *args, **kwargs):
        try:    
            coordinates = kwargs['instance'].point.tuple    #If PointField exists 
            initial = kwargs.get('initial', {})    
            initial['longitude'] = coordinates[0]    #Set longitude from coordinates
            initial['latitude'] = coordinates[1]    #Set Latitude from coordinates
            kwargs['initial'] = initial
        except (KeyError, AttributeError):
            pass
        super().__init__(*args, **kwargs)


来自源PointField的代码,我们看到它的 BaseGeometryWidget .


From the source code of PointField we see that its form class uses the OpenLayersWidget which inherits from the BaseGeometryWidget.

结论是,在此类中,变量

The conclusion is that in this class, the variable display_raw is by default set to False.

如果在yourapp/admin.py中将其设置为True,您将获得一个包含经纬度,经度和其他数据的文本框,可用于调试:

If you set it to True in yourapp/admin.py, you will get a textbox with lat and long and other data, useful for debugging purposes:

from django.contrib.gis import admin
from yourapp.models import YourClass
from django.contrib.gis import forms
from django.contrib.gis.db import models    


class YourClassAdminForm(forms.ModelForm):
    your_attribute = forms.PointField(widget=forms.OSMWidget(attrs={
            'display_raw': True}))

class YourClassAdmin(admin.GeoModelAdmin):
    form = YourClassAdminForm


admin.site.register(YourClass, YourClassAdmin)


还有一种方法永久同时包含地图和人工插入经度/纬度(不仅用于调试) ).


There is also a way to have both a map and manual insertion of longitude / latitude permanently (not only for debugging).

想法是使用FloatFields插入经度/纬度,并使用插入的数据更新PointField.

The idea is to use FloatFields to insert the Longitude/Latitude and update the PointField with the inserted data.

这篇关于Geodjango管理员,不以地图显示点场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆