Can't set PointField default value

1.4k views Asked by At

I'm trying to set the default value of a django.contrib.gis.forms.PointField in my django admin with a customized form like this:

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__'

The place_url initial works perfectly but the position is always [0, 0] by default. Is it a bug from the library or something I'm not doing correctly? Any workaround? Thanks!

1

There are 1 answers

1
Achintya Ranjan Chaudhary On

If it is feasible in your use case you can add that on model as shown below, ill try and test with django and update if it works

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))