Code Monkey home page Code Monkey logo

django-simple-select2's Introduction

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

django-simple-select2's People

Contributors

hartwork avatar jerinpetergeorge avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.