Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Open
macolo opened this issue Feb 2, 2018 · 27 comments

Comments

@macolo
Copy link

macolo commented Feb 2, 2018

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
Copy link
Owner

mrts commented Feb 3, 2018

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
Copy link

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

@mrts
Copy link
Owner

mrts commented Nov 12, 2018

@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
Copy link

kigawas commented Dec 27, 2018

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
Copy link
Owner

mrts commented Dec 27, 2018

@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
Copy link

kigawas commented Dec 28, 2018

@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:

  • Implementation of Django's Ajax searchable widget
  • About an example usage of the custom widget mentioned above, you can check this repo

@mrts
Copy link
Owner

mrts commented Dec 28, 2018

@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
Copy link

MHM5000 commented Jan 16, 2019

Anybody working on this?

@mrts
Copy link
Owner

mrts commented Jan 16, 2019

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

@merwok
Copy link

merwok commented Jan 16, 2019

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
Copy link
Owner

mrts commented Apr 23, 2020

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
Copy link

merwok commented Apr 23, 2020

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
Copy link
Author

macolo commented Apr 23, 2020

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

@macolo macolo closed this as completed Apr 23, 2020
@macolo macolo reopened this Apr 23, 2020
@mrts
Copy link
Owner

mrts commented Apr 23, 2020

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
Copy link

farhan0581 commented Apr 25, 2020

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
Copy link
Owner

mrts commented Apr 26, 2020

Great :)!

I propose the following action plan then:

  • I will set up a new organization django-admin-list-filter-addons and add you and me as owners
  • I will move the current repository to django-admin-list-filter-addons/django-admin-list-filter-addons (we will keep the stars 🌟 this way 😉)
  • I will merge your code, update the README and verify that everything works well together, maybe add tests.

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
Copy link
Owner

mrts commented Sep 3, 2020

@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
Copy link

qcoumes commented Oct 23, 2020

@farhan0581 @mrts, Any news about the merge ?

@mrts
Copy link
Owner

mrts commented Oct 23, 2020

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

@dmwyatt
Copy link

dmwyatt commented Dec 2, 2020

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

@mrts
Copy link
Owner

mrts commented Dec 7, 2020

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

@mrts
Copy link
Owner

mrts commented Jan 9, 2021

@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
Copy link

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
Copy link
Owner

mrts commented Jan 9, 2021

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

@ryanhiebert
Copy link

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

@mrts
Copy link
Owner

mrts commented Oct 20, 2023

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.

@paulocoutinhox
Copy link

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests