1

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!

E-Kami
  • 2,529
  • 5
  • 30
  • 50

1 Answers1

0

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))
  • 1
    Thanks but I don't want to set this in the model since my form is only used in the admin and adding a default in the model would mean all my objects created in my app would get this value, which is not what I want – E-Kami Jul 26 '20 at 12:34