makinacorpus / django-geojson

django-geojson is a collection of helpers to (de)serialize (Geo)Django objects into GeoJSON.
GNU Lesser General Public License v3.0
259 stars 69 forks source link

Leaflet widget is not rendered automatically for forms.Forms #108

Open faaizajaz opened 4 years ago

faaizajaz commented 4 years ago

Hi, and thanks for this fantastic project (and django-leaflet).

Using the leaflet widget on modelForms (where the model has a corresponding geom field, e.g. PointField()) works as expected, however when using forms.Form the behavior is not consistent.

The docs say that form widgets are rendered with Leaflet automatically if django-leaflet is available, but this does not seem to be the case.

Taking a simple example:

class SampleForm(forms.Form): location = djgeojson.fields.PointField()

From the docs, we expect that when this form is rendered, there will be a leaflet widget for the location field, which does not seem to work.

Gagaro commented 4 years ago

GeoJSONField is the model fields. You must use GeoJSONFormField in a form.

class SampleForm(forms.Form):
    location = djgeojson.fields.GeoJSONFormField(geom_type='POINT')
faaizajaz commented 4 years ago

Thanks Gagaro, this works and looking at the djgeojson/fields.py I can see why. It's not immediately obvious to me why this is not required when using forms.ModelForm. In that case, the PointField is automatically rendered as a leaflet widget, which is the behavior expected from reading the docs. I guess this has something to do with forms.ModelForm itself?

Perhaps it would be useful to update the documentation for both the ModelForm and Form use cases?

Gagaro commented 4 years ago

I'm not sure why it would work in some cases.

Feel free to update the docs and do a PR or point where in the doc it is, I'll try to update it when I find some time.

faaizajaz commented 4 years ago

Sure, I will take some time this week to test and update myself.