Skip to content

Automatically register EnumFieldListFilter #112

@ckrybus

Description

@ckrybus

Right now we need to use EnumFieldListFilter like this:

from enumfields.admin import EnumFieldListFilter

class MyModelAdmin(admin.ModelAdmin):
    list_filter = [('color', EnumFieldListFilter)]

It's easy to forget it (see #75 ).

But there is a better way. By using django's FieldListFilter.register we can "attach" the filter to the EnumField:

from django.contrib.admin.filters import FieldListFilter
from .fields import EnumFieldMixin

class EnumFieldListFilter(ChoicesFieldListFilter):
    ....

FieldListFilter.register(lambda f: isinstance(f, EnumFieldMixin), EnumFieldListFilter)

This should work with both EnumField and EnumIntegerField fields. The EnumFieldListFilter section in the README won't be needed anymore.

This should work for django > 1.4. For older versions there is a workaround.

Would you be interested in a pull request? These days I try to ask beforehand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions