makinacorpus / django-leaflet

Use Leaflet in your Django projects
GNU Lesser General Public License v3.0
710 stars 281 forks source link

custom widget not used #151

Open ghost opened 8 years ago

ghost commented 8 years ago

I tried to subclass the default LeafletWidget with a custom geometry_field_class.

If you need a reusable customization of widgets maps, first override the JavaScript field behaviour by extending L.GeometryField, then in Django subclass the LeafletWidget to specify the custom geometry_field_class.

my forms.py

from django import forms

from leaflet.forms.widgets import LeafletWidget

from .models import POI

class GeocodingWidget(LeafletWidget):
    geometry_field_class = 'GeocodingField'
    template_name = 'asdf.html'

class POIForm(forms.ModelForm):

    class Meta:
        model = POI
        fields = ('geom',)
        widgets = {'geom': GeocodingWidget()}

    class Media:
        js = ('leaflet.js',)

As you see, I've even tried to set a different template. When I inspect the map widget in the backend, I don't see my custom GeocodingField used. I'm using latest Django 1.9.

2016-03-21-230947_2720x1024_scrot

Gagaro commented 7 years ago

The widget needs to be set on the AdminModel as well. It isn't a good design though, your way should work without having to do that (as it keeps from using several widgets as well..).