GithubHelp home page GithubHelp logo

shinjjang / django-admin-multiple-choice-list-filter Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ctxis/django-admin-multiple-choice-list-filter

1.0 2.0 0.0 28 KB

License: BSD 2-Clause "Simplified" License

Python 75.02% HTML 24.98%

django-admin-multiple-choice-list-filter's Introduction

Django Admin Multiple Choice List Filter

The SimpleListFilter that ships with Django only allows you to filter on one option at a time. MultipleChoiceListFilter extends SimpleListFilter to allow you to filter on multiple options.

Getting started

Install via pip:

pip install django-admin-multiple-choice-list-filter

Add to INSTALLED_APPS in settings.py:

# project/settings.py

INSTALLED_APPS = [
    ...
    'django_admin_multiple_choice_list_filter',
]

As an example, let's say you had a shop app. In that app you have an Order model with a status field that has limited choices:

# shop/models.py

from django.db import models


class Statuses(object):
    RECEIVED, PROCESSING, SHIPPED, CLOSED = range(0, 4)

    CHOICES = (
        (RECEIVED, 'Received'),
        (PROCESSING, 'Processing'),
        (SHIPPED, 'Shipped'),
        (CLOSED, 'Closed'),
    )


class Order(models.Model):
    status = models.IntegerField(
        choices=Statuses.CHOICES,
        default=Statuses.RECEIVED,
    )

Then, in your app's admin.py:

# shop/admin.py

from django.contrib import admin

from django_admin_multiple_choice_list_filter.list_filters import MultipleChoiceListFilter

from .models import Order, Statuses


class StatusListFilter(MultipleChoiceListFilter):
    title = 'Status'
    parameter_name = 'status__in'

    def lookups(self, request, model_admin):
        return Statuses.CHOICES


class OrderAdmin(admin.ModelAdmin):
    list_display = ('status',)
    list_filter = (StatusListFilter,)

admin.site.register(Order, OrderAdmin)

Your admin area will now display the MultipleChoiceListFilter. It looks a lot like the SimpleListFilter, except there is now an additional link next to each choice. Use these links to include or exclude the choice from the results. You can mix and match any combination you like.

https://raw.githubusercontent.com/ctxis/django-admin-multiple-choice-list-filter/master/django-admin-multiple-choice-list-filter.png

You can override the default template in one of two ways.

  1. Override the template: https://docs.djangoproject.com/en/2.0/howto/overriding-templates/. The default template location is django_admin_multiple_choice_list_filter/filter.html
  2. Set the template name in your subclass of MultipleChoiceListFilter, e.g.:

# shop/admin.py ...

class StatusListFilter(MultipleChoiceListFilter):
template = 'path/to/your/template.html' ...

django-admin-multiple-choice-list-filter's People

Contributors

discopatrick avatar

Stargazers

 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.