shamanu4 / dal_admin_filters

Django autocomplete light filters for django admin

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'NoneType' object is not subscriptable

erikdrums opened this issue · comments

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.

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

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},
        )

I've opened a PR on dal to make widget rendering a bit more robust to handle this case yourlabs/django-autocomplete-light#860

Also this #12 will make setting the id a lot easier