Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Automatically register EnumFieldListFilter #112

Open
ckrybus opened this issue Apr 6, 2020 · 0 comments
Open

Automatically register EnumFieldListFilter #112

ckrybus opened this issue Apr 6, 2020 · 0 comments

Comments

@ckrybus
Copy link

ckrybus commented Apr 6, 2020

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant