An autocompleting select widget for Django
Project description
A simple jQueryUI autocomplete <select> replacement for Django.
This project is meant to be a dead simple drop-in replacement for the Select widget when you have too many objects to sensibly use it.
Features
Good “does nots”:
Doesn’t apply styling except on the autocomplete popup list. You get a bare text input widget to work with.
Doesn’t require any SQL tables.
Doesn’t require any subclassing to use.
Doesn’t change your POSTdata.
Not-strictly-good “does nots”:
Doesn’t look pretty (again, it is focused on having as little styling as possible so you can tailor it for your look and feel)
Doesn’t have any MultiSelect functionality (and at this point there are no plans for it)
Doesn’t have any API documentation (yet!)
This is in extremely early alpha status; I’ve only put it on PyPI for the sake of the project I’ve created for.
Quickstart
pip install django-simple-select
Add simpleselect to INSTALLED_APPS in your project’s settings.py.
Add this to your urls.py
# other urls... url('^', include('simpleselect.urls')), # ...
In your form, make use of a simpleselect.AutocompleteSelect in a ModelChoiceField, e.g.
class MyForm(django.forms.ModelForm): class Meta: model = models.Person widgets = { 'employer': simpleselect.AutocompleteSelect( ['name__icontains']), } class AddPersonForm(django.forms.ModelForm): _personwidget = simpleselect.AutocompleteSelect( ['first_name__icontains', 'last_name__icontains']) person = django.forms.ModelChoiceField(models.Person.objects.all(), widget=_personwidget)
The autocomplete widget takes a list of Django field lookups. When autocompleting, it splits the search string. For each individual word, it ORs together all lookups applied ot that word. It then ANDs together the results of that process applied to all words.
E.g.: for a widget like
simpleselect.AutocompleteSelect(['first_name__icontains', 'last_name__icontains'])
and a search string like
"John Smi"
your final query is equivalent to this, built using Django Q objects:
((Q(first_name__icontains='John') | Q(last_name__icontains='John')) & (Q(first_name__icontains='Smi') | Q(last_name__icontains='Smi')))
This seems to be the correct thing to do, at least most of the time. But I am no expert. More documentation coming!
Add this to your template
{% load staticfiles %} <script type=
"text/javascript"src="{% static "simpleselect.js" %}"></script>Unless I forgot something else while writing this, you should be good to go!
Discussion/help
For now, feel free to message me directly on Github or open a ticket. There’s no mailing list or anything fancy like that. If this picks up any steam I’ll add that stuff.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for django-simple-select-0.1.8.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82a8f4e1d8f161c337cfe4c954b66d89249f935d8296dd174a38e829365aeb5a |
|
MD5 | 37aef3c069e5c5c0c0c0611e5b8951c7 |
|
BLAKE2b-256 | fa3b8488762db32f186eac9e9bd66723c852ea2e124329950a7f1f93834a7cd0 |