Giter Site home page Giter Site logo

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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

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

choices as model

I don't have my status choices hard like you have in the example, mine are their own model, set in the database. How do I use this package with that?

models.py

class ApplicantStatus(models.Model):
    status = models.CharField(verbose_name="Status", max_length=100, blank=False)
    text_color = models.CharField(verbose_name="Tekstkleur", max_length=7, blank=False, default="#333333")
    background_color = models.CharField(verbose_name="Achtergrondskleur", max_length=7, default="#FFFFFF")

    def __str__(self):
        return self.status

    class Meta:
        verbose_name = ' sollicitant status'
        verbose_name_plural = 'Sollicitant statussen'


class AbstractApplicant(models.Model):
    vacancy = models.ForeignKey(Vacancy, on_delete=DO_NOTHING, verbose_name="Vacature", null=True, blank=True)
    initials = models.CharField(verbose_name="Voorletters", max_length=25, null=True, blank=True)
    first_name = models.CharField(verbose_name="Voornaam", max_length=25, null=True, blank=True)
    last_name = models.CharField(verbose_name="Achternaam", max_length=25, null=True, blank=True)
    sex = models.CharField(verbose_name="Geslacht", choices=SEX_CHOICES, max_length=10, blank=False, null=False, default="Unspecified",)
    status = models.ForeignKey(ApplicantStatus, on_delete=DO_NOTHING, verbose_name="Status", default=1, null=True, blank=True)
    date_of_birth = models.DateField(verbose_name="Geboortedatum", null=True, blank=True)
    phone_number = models.CharField(max_length=20, verbose_name="Telefoonnummer", null=True, blank=True)
    address = models.CharField(max_length=30, verbose_name="Adres", null=True, blank=True)
    zip_code = models.CharField(verbose_name="Postcode", max_length=7, validators=[validate_postal_code], null=True, blank=True)
    city = models.CharField(max_length=20, verbose_name="Woonplaats", null=True, blank=True)
    email = models.EmailField(verbose_name="E-mail", null=True, blank=True)
    drivers_license = models.CharField(verbose_name="Rijbewijs", max_length=200, blank=True, null=True)
    curriculum_vitae = models.FileField(verbose_name='Curriculum vitae (pdf)', max_length=1000, upload_to=applicant_directory_path,
                                        validators=[FileExtensionValidator(allowed_extensions=['pdf', 'doc', 'docx'])])
    motivational_letter = models.FileField(verbose_name='Motivatiebrief (pdf)', max_length=1000, upload_to=applicant_directory_path,
                                           validators=[FileExtensionValidator(allowed_extensions=['pdf', 'doc', 'docx'])])
    hours_min = models.IntegerField(verbose_name='Uren (minimaal)', blank=True, null=True, default=0, validators=[MaxValueValidator(40), MinValueValidator(0)])
    hours_max = models.IntegerField(verbose_name='Uren (maximaal)', blank=True, null=True, default=40, validators=[MaxValueValidator(40), MinValueValidator(0)])
    salary = models.IntegerField(verbose_name='Gewenst bruto maandsalaris', blank=True, null=True, default=0, validators=[MaxValueValidator(10000), MinValueValidator(0)])
    social_media = models.CharField(verbose_name='Social media', max_length=255, blank=True, null=True, default='')
    referrer = models.ForeignKey(ApplicantReferrerChoice, on_delete=DO_NOTHING, verbose_name="Hoe heeft u deze vacature gevonden?", default=1, blank=True)
    referrer_other = models.CharField(verbose_name="Anders, namelijk:", blank=True, null=True, max_length=255)
    comment = models.TextField(verbose_name='Opmerking', blank=True, null=True)
    agreed_to_privacy_policy = models.BooleanField(default=False, )

    created_at = models.DateTimeField(verbose_name='Gesolliciteerd op', auto_now_add=True)
    updated_at = models.DateTimeField(verbose_name='Laatst bewerkt op', auto_now=True)

    viewed = models.BooleanField(verbose_name="Nieuw", default=False)

    def __str__(self):
        return '%s %s' % (self.first_name, self.last_name)

    class Meta:
        abstract = True

admin.py

class ApplicantListFilter(MultipleChoiceListFilter):
    title = 'Status'
    parameter_name = 'status'

    @staticmethod
    def lookups(request, model_admin):
        return Applicant.status


I get an 'ForwardManyToOneDescriptor' object is not iterable. How does that work?

Is this project maintained?

Hi, it would be great if the project can move forward, either allow other people in, or transfer it to Jazzband

Custom filter without parameter_name

Right now, it seems to me that I absolutely must have a parameter_name, but what if I want to filter not by a field, but by a method in the model? Is this possible?

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.