I am trying to implement Django Leaflet into my Wagtail app. The underlying form field is a GeoJSON field. I only get it to work properly when registering my model as a snippet not as a Page
model, though.
I can add instances of my model based on the Page
model and the GeoJSON value is written correctly to the database. I can also edit a features geometry but the geometry won't be displayed on the leaflet map. When Wagtail renders the edit view for the Page
based model the GeoJSON turns into a string and deserialization fails:
Error creating geometry from value
'"{\"type\":\"Point\",\"coordinates\":[-322.276779,59.41526]}"'
(String input unrecognized as WKT EWKT, and HEXEWKB.)
Loading the edit view for the same model registered as a snippet works, the JSON stays a dict
and the geometry gets rendered on the leaflet map. So I suspect somewhere Wagtails edit handler does something to the context
it doesn't do when handling snippets. Where to look and how to prevent it?
I found a solution. I followed the error message to it’s method and overwrote it. My widget looks like this now:
[...]
from leaflet.forms.widgets import LeafletWidget
class LeafletWidgetWithMedia(LeafletWidget):
include_media = True
template_name = "widget.html"
def deserialize(self, value):
try:
value = json.loads(value) # this is the part I added
return GEOSGeometry(value)
except (GEOSException, ValueError, TypeError) as err:
logger.error("Error creating geometry from value '%s' (%s)", value, err)
return None
I am still curious though what happens internally on Wagtails side.