Skip to content

jerinpetergeorge/django-simple-select2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Tweaks for Django's built-in autocomplete feature

Resources

License

Stars

Watchers

Forks

Packages

No packages published