Skip to main content

Django-select2-rocks

Light integration glue between Django and Select2.

This project is inspired by Django-Select2 and django-select2light.

Django-select2-rocks is distributed under the BSD 2-clause license.

Installation

  1. pip install django_select2_rocks

  2. add select2rocks to your INSTALL_APPS

  3. python manage.py collectstatic will install Django Select2 Rocks JS.

  4. Include jQuery (1.7+), Select2 JS/CSS (not provided, tested with v3.4.5) and select2rocks/select2rocks.js in your templates.

  5. Now, you can use Select2ModelChoiceField fields in your forms.

Design

Django-select2-rocks provides widgets to render and initialize Select2 inputs.

The widget rendering is based on Django with a further step which adds a JS initialization for each input (on DOM ready). When a character is typed in the input field, an AJAX request is sent to the URL associated to the field.

The widget API is designed to allow you to pass any arguments you want to Select2 JS code (eg. allowClear option).

You can use various JSON views to answer to Select2 AJAX queries, and so to adjust search terms or the format of the results, it’s possible to extend the default django-select2-rocks backend (see select2rocks-backends.js in testproj for an example).

Example

The testproj project contains examples with a simple JSON view and a Django REST framework view.

import select2rocks

class BeachForm(forms.Form):
    beach = select2rocks.Select2ModelChoiceField(
        queryset=Beach.objects.all(),
        widget=select2rocks.AjaxSelect2Widget(url_name='json_beaches'))

Subtleties

Beware, the internal representation of the fields uses commas (,) to separate items. Thus you cannot use commas in your labels. Otherwise we’ll interpret it as a separator and try to work with the text on both sides of the comma, which will most likely break.

Get the Code

Django-select2-rocks is developed on GitHub:

https://github.com/polyconseil/django-select2-rocks

You can either clone the public repository:

$ git clone git://github.com/polyconseil/django-select2-rocks.git

Once you have a copy of the source, you can install it with:

$ python setup.py install

Metadata

Release files for django-select2-rocks 0.9.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-select2-rocks 0.9.0
File Size Uploaded
django-select2-rocks-0.9.0.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-select2-rocks 0.9.0
File Interpreter ABI Platform
django_select2_rocks-0.9.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.2 kB

Release files / django-select2-rocks-0.9.0.tar.gz

Download URL django-select2-rocks-0.9.0.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
b0b0788239e197ce272e2e0c751a2dc30064e242dc9e98adc5fb422030cf98e5
BLAKE2b-256 checksum
How to use checksums
8ff9434b58a2e3f7922887762737cbc104807c6337472c108699436e998dc016
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release files / django_select2_rocks-0.9.0-py3-none-any.whl

Download URL django_select2_rocks-0.9.0-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
028c1fbcfeb8ffb6b68391944319a2bb42aefb0dccc5a844e4308d9db20ebb58
BLAKE2b-256 checksum
How to use checksums
d93f19103950d98d3e6580dcb64c9f855e992722c92c0c403b69b2162ebadbaf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.18

Release history Release notifications | RSS feed

This release

0.9.0 This release

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.1

2 release files

0.4

2 release files

0.3

2 release files

0.2

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page