【问题标题】:Can't set PointField default value无法设置 PointField 默认值
【发布时间】:2020-07-25 00:48:10
【问题描述】:

我正在尝试在我的 django 管理员中使用如下自定义表单设置 django.contrib.gis.forms.PointField 的默认值:

from django.contrib.gis import forms
from django.contrib.gis.geos import Point
from api import models


class CustomPlaceCreationForm(forms.ModelForm):
    place_url = forms.CharField(initial="https://www.places.com/myplace")
    position = forms.PointField(
        widget=forms.OSMWidget(attrs={'map_width': 800, 'map_height': 500}),
        initial=Point(x=121.502020, y=25.039270, srid=4326)
    )

    class Meta:
        model = models.Place
        fields = '__all__'

place_url initial 完美运行,但 position 默认始终为 [0, 0]。 是图书馆的错误还是我做的不对?任何解决方法?谢谢!

【问题讨论】:

    标签: python django gis postgis django-geoposition


    【解决方案1】:

    如果在您的用例中可行,您可以将其添加到模型上,如下所示,我尝试使用 django 进行测试,如果可行则更新

    class Client(models.Model):
        firstname = models.CharField(max_length=100)
        lastname = models.CharField(max_length=100)
        location = models.PointField(geography=True, default=Point(0.0, 0.0))
    

    【讨论】:

    • 谢谢,但我不想在模型中设置它,因为我的表单仅在管理员中使用,并且在模型中添加默认值意味着我在我的应用程序中创建的所有对象都将获得此值,这不是我想要的
    猜你喜欢
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-29
    • 2020-05-19
    • 1970-01-01
    • 2023-04-01
    • 2014-06-23
    相关资源
    最近更新 更多