django-search-admin-autocomplete icon indicating copy to clipboard operation
django-search-admin-autocomplete copied to clipboard

Refactor as Mixin

Open lukasburg opened this issue 3 years ago • 4 comments

Just a small suggestion.

Maybe you could refactor your admin class to be used as a Mixin rather than as BaseClass. Like DjangoQl does. So instead of writing

class MyModelAdmin(SearchAutoCompleteAdmin)
    search_fields = ['search_field', ]

you'd write

class MyModelAdmin(SearchAutoCompleteMixin, admin.ModelAdmin)
    search_fields = ['search_field', ]

That way your functionality could probably also be used when using other admin extensions (e.g. [django-nested-admin])(https://pypi.org/project/django-nested-admin/).

I know, multiple inheritance is a controversial topic. But in Django its used all the time (see class based views) so why not use it here?

Correct me, if I'm wrong, but I think the only thing needed for this would be to simply drop the inheritance from this line from class SearchAutoCompleteAdmin(admin.ModelAdmin): to class SearchAutoCompleteAdmin:. And maybe a custom error message on this classes __init__ if it gets used without importing admin.

Tell me, what you think :)

lukasburg avatar Apr 23 '21 13:04 lukasburg

I went ahead and forked this repo to test this. Seems to work just fine: https://github.com/lukasburg/django-search-admin-autocomplete

Should I make a pull request?

lukasburg avatar Apr 23 '21 13:04 lukasburg

@lukasburg That makes sense, please create a PR.

linevych avatar Apr 26 '21 16:04 linevych

Sorry, took some time. I have created a PR.

I have not edited the README or done anything else than the bare minimum to get it running. I free some time to do more edits, if you want me to :)

lukasburg avatar May 04 '21 09:05 lukasburg

Great, thanks. I will take a look this week when I have time.

linevych avatar May 04 '21 09:05 linevych