shamanu4 / dal_admin_filters

Django autocomplete light filters for django admin
MIT License
65 stars 30 forks source link

'NoneType' object is not subscriptable #10

Open erikdrums opened 7 years ago

erikdrums commented 7 years ago

Following along and implementing the example I get "'NoneType' object is not subscriptable". https://github.com/yourlabs/django-autocomplete-light/blob/master/src/dal/widgets.py#L133 expects attrs to have an 'id' key. It defaults to None and doesn't check before calling the 'id' key. https://github.com/shamanu4/dal_admin_filters/blob/master/dal_admin_filters/__init__.py#L47 doesn't explicitly set attrs. I haven't looked too much into it. For me everything works if I set a bogus 'id'.

self.rendered_widget = field.widget.render(
    name=self.parameter_name, 
    value=self.used_parameters.get(self.parameter_name, ''), 
    attrs={'id': 1'},
)

But it of course it is not really the way to go.

olivierdalang commented 7 years ago

Same problem here. @erikdrums did you have to patch the library or where does your fix go ?

erikdrums commented 7 years ago

This works for me @olivierdalang: I haven't tested this exact code, but you get the idea.

from dal_admin_filters import AutocompleteFilter 
from dal import autocomplete

class CustomAutocompleteFilter(AutocompleteFilter):
    def __init__(self, request, params, model, model_admin):
        super(AutocompleteFilter, self).__init__(request, params, model, model_admin)
        self._add_media(model_admin)

        field = forms.ModelChoiceField(
            queryset=getattr(model, self.parameter_name).get_queryset(),
            widget=autocomplete.ModelSelect2(
                url=self.autocomplete_url,
            )
        )

        self.rendered_widget = field.widget.render(
            name=self.parameter_name, 
            value=self.used_parameters.get(self.parameter_name, ''), 
            attrs={'id':1},
        )
xrmx commented 7 years ago

I've opened a PR on dal to make widget rendering a bit more robust to handle this case https://github.com/yourlabs/django-autocomplete-light/pull/860

xrmx commented 7 years ago

Also this https://github.com/shamanu4/dal_admin_filters/pull/12 will make setting the id a lot easier