mlavin / django-selectable

Tools and widgets for using/creating auto-complete selection widgets using Django and jQuery UI.
http://django-selectable.readthedocs.io/en/latest/
BSD 2-Clause "Simplified" License
129 stars 64 forks source link

missing filter in get_item to find correct item #191

Open TZanke opened 7 years ago

TZanke commented 7 years ago

I have the following problem. I need the query_parameters to get the correct item in get_item. atm it does not look like if it is supported. So i implemented this workaround:

class CustomWidget(AutoComboboxSelectWidget):
    def update_query_parameters(self, qs_dict):
        self.lookup_class.custom_filter.update(qs_dict)
        super(AutoComboboxSelect, self).update_query_parameters(qs_dict)
class ABCLookup(ModelLookup):
    custom_filter = dict()
    def get_item(self, value):
        return self.get_queryset().filter(**self.custom_filter).get(number=value)

Works, but good idea? Any other solution?

mlavin commented 7 years ago

Hmm. get_item is used by the field's to_python method as well and the request isn't really available there. I'm not sure it can be that strictly enforced but maybe I'm forgetting something.