Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'NoneType' object is not subscriptable #10

Open
erikdrums opened this issue Mar 29, 2017 · 4 comments
Open

'NoneType' object is not subscriptable #10

erikdrums opened this issue Mar 29, 2017 · 4 comments

Comments

@erikdrums
Copy link

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
Copy link

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

@erikdrums
Copy link
Author

erikdrums commented May 9, 2017

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
Copy link
Contributor

xrmx commented Jun 7, 2017

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

@xrmx
Copy link
Contributor

xrmx commented Jun 7, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants