App enabling the use of jQuery UI autocomplete widget for ModelChoiceFields with minimal configuration required.
Project description
Django Simple Autocomplete
App enabling the use of jQuery UI autocomplete widget for ModelChoiceFields with minimal configuration required.
Installation
Install or add django-simple-autocomplete to your Python path.
Add simple_autocomplete to your INSTALLED_APPS setting.
Add (r’^simple-autocomplete/’, include(‘simple_autocomplete.urls’)) to urlpatterns.
Ensure jQuery core, jQuery UI Javascript and jQuery UI CSS is loaded by your templates. Your jQueryUI bundle must include the autocomplete widget described at http://docs.jquery.com/UI/Autocomplete.
Usage
Django by default renders a select widget (a.k.a. combobox or dropdown) for foreign key fields. You can change the widget to an autocomplete widget by adding the model to the legacy SIMPLE_AUTOCOMPLETE_MODELS tuple in your settings file. For instance, to use the autocomplete widget when selecting a user do:
SIMPLE_AUTOCOMPLETE_MODELS = ('auth.user',)
For more parameters set the preferred SIMPLE_AUTOCOMPLETE dictionary in your settings file. The example above then becomes:
SIMPLE_AUTOCOMPLETE = {'auth.user': {'threshold': 10}}
The dictionary format allows arbitrary parameters to be introduced in future. Parameter threshold indicates the minimum number of options required before the widget is rendered as an autocomplete widget. If the threshold is not met the default widget is rendered.
Parameter max_items indicates the maximum number of matches to display in the autocomplete dropdown. It defaults to 10.:
SIMPLE_AUTOCOMPLETE = {'auth.user': {'max_items': 10}}
Parameter duplicate_format_function is a lambda function that enables a custom string should more than one item in the autocomplete dropdown have the same string value. It defaults to displaying the content type name. Set it using a lambda function, eg.:
SIMPLE_AUTOCOMPLETE = {'auth.user': {'duplicate_format_function': lambda obj, model, content_type: 'id: %s' % obj.id}}
The product attempts to use a field title for filtering the list. If the model has no field title then the first CharField is used. Eg. for the user model the field username is used.
The widget can be used implicitly in a form. The declaration of ModelChoiceField is all that is required:
class MyForm(forms.Form): user = forms.ModelChoiceField(queryset=User.objects.all(), initial=3)
The widget can be used explicitly in a form. In such a case you must provide an URL which returns results as JSON with format [(value, label), (value, label),…]. The initial and initial_display parameters are only required if there is a starting value:
from simple_autocomplete.widgets import AutoCompleteWidget class MyForm(forms.Form): user = forms.ModelChoiceField( queryset=User.objects.all(), initial=3, widget=AutoCompleteWidget( url='/custom-json-query', initial_display='John Smith' ) )
The ability to specify an URL for the widget enables you to hook up to other more advanced autocomplete query engines if you wish.
Changelog
0.3.3
Tests failing for Django 1.5. Pin to 1.4.x until that is fixed.
Handle case where an item that is referenced by a multiselect has been deleted from the database.
0.3.2
Allow search_field to be specified per model, in case the defaults don’t suffice.
0.3.1
Fix unicode bug.
0.3
max_items setting specifies maximum number of items to display in autocomplete dropdown.
duplicate_format_function setting allows appending of a custom string if more than one item in the autocomplete dropdown has the same string value.
0.2
Clear autoselect helper in some cases for cleaner UI.
Use object string representation for display and not lookup fieldname.
0.1
Add autocomplete widget for multiple selections
Threshold setting to determine when to show autocomplete widget instead of normal widget
0.0.1
Initial release.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file django-simple-autocomplete-0.3.3.tar.gz
.
File metadata
- Download URL: django-simple-autocomplete-0.3.3.tar.gz
- Upload date:
- Size: 15.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7a65d3c637b5aa1de0972ebddf3bc69b01a714d61ec90474673b3a8387274484 |
|
MD5 | 7c345ca2ce65f5d8607d39fa49a84684 |
|
BLAKE2b-256 | c1afc414a2f52ef4f43c6d538192a398d861ca49fe7cc56445b9ec623377d543 |
Provenance
File details
Details for the file django_simple_autocomplete-0.3.3-py2.7.egg
.
File metadata
- Download URL: django_simple_autocomplete-0.3.3-py2.7.egg
- Upload date:
- Size: 18.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7275f9732ceefce010e088488abe6b2646a4380bc8db42c8900af186ec3f1e5 |
|
MD5 | b21b7951fd84d19003391548f9966adf |
|
BLAKE2b-256 | 9100dfd9c4eba662845cbcaba47523a5da78c9c68516fdd42eb21ffda0fa68a0 |