Skip to main content

Django form fields using the Select2 jQuery plugin

Project description

https://travis-ci.org/theatlantic/django-select2-forms.svg?branch=master

django-select2-forms is a project that makes available Django form fields that use the Select2 javascript plugin. It was created by developers at The Atlantic.

Support

Being that Django added select2 support in 2.0, we will support up to that version for compatibility purposes.

  • ~=v2.0.2: Python ~=2.7,~=3.6 | Django >=1.8,<2.1

  • ~=v2.1: Python ~=2.7,>=3.6,<3.8 | Django >=1.11,<2.1

  • ~=v3.0: __Python >=3.6,<3.9 | Django >=2.0,<2.1 (future release)__

Installation

The recommended way to install is with pip:

pip install django-select2-forms

or, to install with pip from source:

pip install -e git+git://github.com/theatlantic/django-select2-forms.git#egg=django-select2-forms

If the source is already checked out, use setuptools:

python setup.py develop

Configuration

django-select2-forms serves static assets using django.contrib.staticfiles, and so requires that "select2" be added to your settings’ INSTALLED_APPS:

INSTALLED_APPS = (
    # ...
    'select2',
)

To use django-select2-forms’ ajax support, 'select2.urls' must be included in your urls.py urlpatterns:

urlpatterns = patterns('',
    # ...
    url(r'^select2/', include('select2.urls')),
)

Usage

The simplest way to use django-select2-forms is to use select2.fields.ForeignKey and select2.fields.ManyToManyField in place of django.db.models.ForeignKey and django.db.models.ManyToManyField, respectively. These fields extend their django equivalents and take the same arguments, along with extra optional keyword arguments.

select2.fields.ForeignKey examples

In the following two examples, an “entry” is associated with only one author. The example below does not use ajax, but instead performs autocomplete filtering on the client-side using the <option> elements (the labels of which are drawn from Author.__str__()) in an html <select>.

@python_2_unicode_compatible
class Author(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Entry(models.Model):
    author = select2.fields.ForeignKey(Author,
        overlay="Choose an author...",
        on_delete=models.CASCADE)

This more advanced example autocompletes via ajax using the Author.name field and limits the autocomplete search to Author.objects.filter(active=True)

class Author(models.Model):
    name = models.CharField(max_length=100)
    active = models.BooleanField()

class Entry(models.Model):
    author = select2.fields.ForeignKey(Author,
        limit_choices_to=models.Q(active=True),
        ajax=True,
        search_field='name',
        overlay="Choose an author...",
        js_options={
            'quiet_millis': 200,
        },
        on_delete=models.CASCADE)

select2.fields.ManyToManyField examples

In the following basic example, entries can have more than one author. This example does not do author name lookup via ajax, but populates <option> elements in a <select> with Author.__unicode__() for labels.

@python_2_unicode_compatible
class Author(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name

class Entry(models.Model):
    authors = select2.fields.ManyToManyField(Author)

The following “kitchen sink” example allows authors to be ordered, and uses ajax to autocomplete on two variants of an author’s name.

from django.db import models
from django.db.models import Q
import select2.fields
import select2.models

class Author(models.Model):
    name = models.CharField(max_length=100)
    alt_name = models.CharField(max_length=100, blank=True, null=True)

class Entry(models.Model):
    categories = select2.fields.ManyToManyField(Author,
        through='EntryAuthors',
        ajax=True,
        search_field=lambda q: Q(name__icontains=q) | Q(alt_name__icontains=q),
        sort_field='position',
        js_options={'quiet_millis': 200})

form field example

If you don’t need to use the ajax features of django-select2-forms it is possible to use select2 on django forms without modifying your models. The select2 formfields exist in the select2.fields module and have the same class names as their standard django counterparts (ChoiceField, MultipleChoiceField, ModelChoiceField, ModelMultipleChoiceField). Here is the first ForeignKey example above, done with django formfields.

class AuthorManager(models.Manager):
    def as_choices(self):
        for author in self.all():
            yield (author.pk, force_text(author))

@python_2_unicode_compatible
class Author(models.Model):
    name = models.CharField(max_length=100)
    objects = AuthorManager()

    def __str__(self):
        return self.name

class Entry(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE)

class EntryForm(forms.ModelForm):
    author = select2.fields.ChoiceField(
        choices=Author.objects.as_choices(),
        overlay="Choose an author...")

    class Meta:
        model = Entry

License

The django code is licensed under the Simplified BSD License and is copyright The Atlantic Media Company. View the LICENSE file under the root directory for complete license and copyright information.

The Select2 javascript library included is licensed under the Apache Software Foundation License Version 2.0. View the file select2/static/select2/select2/LICENSE for complete license and copyright information about the Select2 javascript library.

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

django-select2-forms-2.1.1.tar.gz (207.5 kB view details)

Uploaded Source

Built Distribution

django_select2_forms-2.1.1-py2.py3-none-any.whl (215.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file django-select2-forms-2.1.1.tar.gz.

File metadata

  • Download URL: django-select2-forms-2.1.1.tar.gz
  • Upload date:
  • Size: 207.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.2

File hashes

Hashes for django-select2-forms-2.1.1.tar.gz
Algorithm Hash digest
SHA256 4459ec23eaea3c49419b9d260daf6dbb755e88c1c8bf930fa44cd60518487e58
MD5 38302090636c796abb765f154b7857b1
BLAKE2b-256 a48fa863e340a286affa6e681940c047aed715cec7d4b5a2ee16e4a11e956eea

See more details on using hashes here.

File details

Details for the file django_select2_forms-2.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_select2_forms-2.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 215.2 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.2

File hashes

Hashes for django_select2_forms-2.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 15180a7a53fecacbc6aaf0daa64d41416d75688f3924c6c970c2326f2684f804
MD5 e45391377daf42391e2e88e9b6038fc9
BLAKE2b-256 74190875d40db47bc3f5c13441f35ead675bcfcf4c60fcf6a4ccf6af05550b51

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page