Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LocationField throws AttributeError because value is always None #145

Open
daMichaelB opened this issue Sep 21, 2020 · 0 comments
Open

LocationField throws AttributeError because value is always None #145

daMichaelB opened this issue Sep 21, 2020 · 0 comments

Comments

@daMichaelB
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant