django-location-field
django-location-field copied to clipboard
LocationField throws AttributeError because value is always None
Hello there. I have a strange behaviour with the LocationField
in my forms.py
.
I guess i do something wrong, but have no idea what it is (maybe the problem is that i use crispy-forms?)
I have a Spatial LocationField
in my model and added it also to my Create-Form.
forms.py
from location_field.forms.spatial import LocationField
class SomeCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['location'].initial = "2.0,2.0"
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.layout = Layout(
...
LocationField("location"),
...
Submit("create", "Create", css_class="some_class"),
)
class Meta:
model = Place
fields = (..., 'location', ...)
Problem:
When LocationField.clean()
is called, the value is always None
, which leads to a AttributeError
.
The initial-value gets propagated to self.initial
in my case.
class LocationField(PlainLocationField):
def clean(self, value):
try:
lat, lng = value.split(',')
return Point(float(lng), float(lat))
except ValueError:
return None
So as a workaround i created a costum MyLocationField()
where i add the initial value directly into the .clean()
function.
I am looking forward to any kind of help or advice here. Thank you!