mrts / django-admin-list-filter-dropdown

Use dropdowns in Django admin list filter
MIT License
315 stars 48 forks source link

Django2 now supports AutoComplete via select2, can django-admin-list-filter-dropdown? #7

Open macolo opened 6 years ago

macolo commented 6 years ago

The Autocomplete URL can be retrieved via a template tag:

register = template.Library()

@register.simple_tag(takes_context=True)
def get_autocomplete_url(context):
    spec = context.get('spec')
    app_label = spec.field.related_model._meta.app_label
    model_name = spec.field.related_model._meta.model_name

    return reverse('admin:{}_{}_autocomplete'.format(
        app_label,
        model_name,
    ))

The problem is to insert admin/js/vendor/select2/select2.full.js after admin/js/vendor/jquery/jquery.js but before /static/admin/js/jquery.init.js which enables noConflict for jQuery and hides jQuery from the global scope (which select2 needs). The filter's __init__ gets ModelAdmin instance passed, however its Media instance cannot be changed at this point. The only option I found is extending the admin's change_list.html template, which adds complexity to the module (needs to be inserted before django.contrib.admin in INSTALLED_APPS.

It works, but its far from being modular.

mrts commented 6 years ago

Excellent, many thanks for the suggestion! I was thinking about this, but didn't look into the implementation yet. I'll take a look at your pull request in near future.

rocchidavide commented 5 years ago

Any news about this feature? It will be implemented? Thanks

mrts commented 5 years ago

@rocchidavide see discussions and code at pull request #8, at the moment it is incomplete and it seems @macolo does not currently have time to finish the implementation. Feel free to help out :)!

kigawas commented 5 years ago

You can add a custom js file, say file name is autoselect.js, like:

(function ($) {
  console.debug($, 'LOADED');

  $(function () {
    $('select[auto-select=1]').select2();
  });

}(django.jQuery));

Then, define a custom widget like:

class AutoSelectWidget(Select):
    @property
    def media(self):
        extra = "" if settings.DEBUG else ".min"
        return Media(
            js=[
                f"admin/js/vendor/jquery/jquery{extra}.js",
                f"admin/js/vendor/select2/select2.full{extra}.js",
                "admin/js/jquery.init.js",
                "js/autoselect.js",
            ]
        )

Finally, use it in a form:

class SomeForm(ModelForm):
    class Meta:
        model = SomeModel
        fields = "__all__"

        widgets = {
            "field":  AutoSelectWidget(attrs={"auto-select": 1})
        }

And get rid of this library to use pure django.

mrts commented 5 years ago

@kigawas How do you envision integrating your solution to Django admin list_filter that this project is about?

That is, what would you write instead of ??? in the following snippet?

class EntityAdmin(admin.ModelAdmin):
    ...
    list_filter = ( ??? )

Note that the solution proposed by @macolo here uses AJAX to fetch select results from the backend - do you have a suggestion how to implement this?

kigawas commented 5 years ago

@kigawas How do you envision integrating your solution to Django admin list_filter that this project is about?

That is, what would you write instead of ??? in the following snippet?

class EntityAdmin(admin.ModelAdmin):
    ...
    list_filter = ( ??? )

Note that the solution proposed by @macolo here uses AJAX to fetch select results from the backend - do you have a suggestion how to implement this?

FYI:

mrts commented 5 years ago

@kigawas Cool, thanks for the links! Would you consider submitting a pull request to add autocomplete functionality? Note that this project is MIT-licensed though, similar to the liberal BSD license of Django itself.

MHM5000 commented 5 years ago

Anybody working on this?

mrts commented 5 years ago

Not at the moment, sorry. Getting it right seems not to be trivial unfortunately. Let's hope a brave champion arises :woman_firefighter:, if not then eventually I hope to get time for this.

merwok commented 5 years ago

To answer this question:

class EntityAdmin(admin.ModelAdmin):
    ...
    list_filter = ( ??? )

This is what I would want:

class EntityAdmin(admin.ModelAdmin):
    list_filter = (("field", AutocompleteFilter),)

(i.e. similar to how one uses SimpleRelatedFilter from django)

I haven’t looked yet in how to achieve that; in a previous project with lots of custom filter, I stopped at the simpler form list_filter = (make_filter("field"), CustomFilterXyz("other")).

mrts commented 4 years ago

As this is large work and I don't currently have time for this, perhaps we could set up a bounty at Issuehunt (or some other bounty site)? Please comment if you support that and how much you would be willing to contribute - or if you would like to pick up the bounty. I'm willing to contribute $20 myself.

merwok commented 4 years ago

FWIW, I have used https://github.com/farhan0581/django-admin-autocomplete-filter in my project and it works pretty well. Thanks nonetheless for writing this one and making it available!

I haven’t looked recently if this package provides features that that other doesn’t, in which case maybe joining forces could be an idea?

macolo commented 4 years ago

Jup that's true, I actually am also using that solution :)

mrts commented 4 years ago

Many thanks for the link @merwok (and thanks to @kigawas once more for suggesting it previously) and cheers for your great work @farhan0581! Would be awesome to collaborate, but Farhan's work is licensed under GPL 3, so I cannot even look at the code for the time being if I want to keep the more liberal MIT license. @farhan0581, what do you think about joining forces? I'm more than happy to merge your code and hand over maintenance.

farhan0581 commented 4 years ago

Yeah sure man ! ,BTW thanks for your great work. It would be great to have someone like you, helping with the project since django is ever evolving which attracts the need for maintenance.

mrts commented 4 years ago

Great :)!

I propose the following action plan then:

Does this sound good? Is it OK for you to redirect farhan0581/django-admin-autocomplete-filter to django-admin-list-filter-addons/django-admin-list-filter-addons? Are you OK with licensing your code with MIT license (which resembles Django's own license)?

I'll wait for your confirmation before proceeding.

mrts commented 4 years ago

@farhan0581, did you have time to think about the plan that I proposed in April? Please feel free to suggest amendments to the plan or decline freely, then I can explore other avenues forward.

qcoumes commented 3 years ago

@farhan0581 @mrts, Any news about the merge ?

mrts commented 3 years ago

@qcoumes, still waiting for @farhan0581's answer on the proposal.

dmwyatt commented 3 years ago

Taking a look at @farhan0581's github activity, I wouldn't expect an answer from him.

mrts commented 3 years ago

I cannot unfortunately merge GPL 3-licensed code, so his opinion is needed - @farhan0581, any news from your side?

mrts commented 3 years ago

@merwok @macolo you mention that you use https://github.com/farhan0581/django-admin-autocomplete-filter. Do you use it in closed-source commercial projects? If yes, then how do you comply with the GPL 3 license? As far as I can understand, using GPL 3 licensed code means that you have to publish the source code of your project as well?

ryanhiebert commented 3 years ago

As long as your project doesn't release software for your users to install, GPL isn't a problem, to my understanding. If you wanted to protect a library from use in a closed source Service, for example, you would want to use the AGPL, not the GPL.

mrts commented 3 years ago

Alright, all good then, thanks for explaining! I have to admit that I haven’t investigated licensing implications thoroughly enough.

ryanhiebert commented 3 years ago

@mrts I'd be up for a chat or call sometime on the subject if you'd find that useful.

mrts commented 11 months ago

I see that https://github.com/shamanu4/dal_admin_filters is MIT-licensed, this could be used for inspiration instead of https://github.com/farhan0581/django-admin-autocomplete-filter.