Skip to main content

Tweaks for existing built-in Django's autocomplete feature

Project description

django-simple-select2

This simple django app enables users to do a few tweaks to Django's built-in autocomplete feature.

Installation

pip install django-simple-select2

Usage

models.py

class Publication(models.Model):
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.name


class Reporter(models.Model):
    full_name = models.CharField(max_length=50)
    email = models.EmailField()

    def __str__(self):
        return self.full_name


class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    publications = models.ManyToManyField(Publication)
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

    def __str__(self):
        return self.headline

admin.py

from django.contrib import admin
from .models import Article
from simple_select2 import Select2Admin, AutoCompleteSelect2, AutoCompleteSelect2Multiple


class ArticleModelAdmin(Select2Admin, admin.ModelAdmin):
    extra = {
        'publications': AutoCompleteSelect2Multiple(url='select2-publication-list'),
        'reporter': AutoCompleteSelect2(url='select2-reporter-list')
    }


admin.site.register(Article, ArticleModelAdmin)

views.py

from simple_select2 import AutoCompleteBaseView
from .models import Reporter, Publication


class ReporterView(AutoCompleteBaseView):
    model = Reporter
    search_fields = ('full_name', 'email')


class PublicationView(AutoCompleteBaseView):
    model = Publication
    search_fields = ('name',)

#urls.py
from django.urls import path
from .views import ReporterView, PublicationView

urlpatterns = [
    path('reporter/', ReporterView.as_view(), name='select2-reporter-list'),
    path('publication/', PublicationView.as_view(), name='select2-publication-list'),
    ...
]

Settings

SIMPLE_SELECT2_THEME

Sets the project-wide default theme to be used by Select2 for all widgets inheriting from AutoCompleteSelect2Mixin. Can be overridden per widget using parameter theme.

Supported values are:

  • None (or unset) will use theme "admin-autocomplete". This is the default.
  • "admin-autocomplete" uses the theme of Django Admin.
  • "bootstrap4" uses a bundled copy of Takashi Kanemoto's select2-bootstrap4-theme. Please note that this theme requires that you pull in Bootstrap 4 CSS and JavaScript assets in your templates somewhere yourself.
  • "classic" uses the old classic theme of upstream Select2. Not much different from theme "admin-autocomplete".
  • "default" uses the default upstream theme of Select2.

SIMPLE_SELECT2_WIDTH

Sets the project-wide default width to be used by Select2 for all widgets inheriting from AutoCompleteSelect2Mixin. Can be overridden per widget using parameter width.

For supported values, please check the official documentation of parameter width of Select2. By default, django-simple-select2 does not enforce any width on Select2.

Demo

You will find a simple demo app here, simple-select2-demo

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-simple-select2-0.2.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

django_simple_select2-0.2.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file django-simple-select2-0.2.0.tar.gz.

File metadata

  • Download URL: django-simple-select2-0.2.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9

File hashes

Hashes for django-simple-select2-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d5473b41776dc178588631aa92bf7638256221424149ae2e855a02f64e3e4e3f
MD5 aa8ffe5fda3aaa2c5b9b5e345c03f3ed
BLAKE2b-256 9e4c57b1ec6159221d86cd99e7c84a6da2eaa00b636817fb889428f95c369f47

See more details on using hashes here.

File details

Details for the file django_simple_select2-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: django_simple_select2-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.6.9

File hashes

Hashes for django_simple_select2-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f92c3dfb44a95955de14934ef6ee56d2230ffa4eff3e11dc84ba53d2d02a600
MD5 80771aea51faaf8939a5ec0fe25eee34
BLAKE2b-256 11a6b36f77e0f542d26c56da6ba1ddab1ef6c1dbc02eba2d509ffcf9d745f704

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