Open macolo opened 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.
Any news about this feature? It will be implemented? Thanks
@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 :)!
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.
@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 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:
@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.
Anybody working on this?
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.
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"))
.
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.
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?
Jup that's true, I actually am also using that solution :)
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.
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.
Great :)!
I propose the following action plan then:
django-admin-list-filter-addons
and add you and me as ownersdjango-admin-list-filter-addons/django-admin-list-filter-addons
(we will keep the stars :star2: this way :wink:)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.
@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.
@farhan0581 @mrts, Any news about the merge ?
@qcoumes, still waiting for @farhan0581's answer on the proposal.
Taking a look at @farhan0581's github activity, I wouldn't expect an answer from him.
I cannot unfortunately merge GPL 3-licensed code, so his opinion is needed - @farhan0581, any news from your side?
@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?
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.
Alright, all good then, thanks for explaining! I have to admit that I haven’t investigated licensing implications thoroughly enough.
@mrts I'd be up for a chat or call sometime on the subject if you'd find that useful.
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.
The Autocomplete URL can be retrieved via a template tag:
The problem is to insert
admin/js/vendor/select2/select2.full.js
afteradmin/js/vendor/jquery/jquery.js
but before/static/admin/js/jquery.init.js
which enablesnoConflict
for jQuery and hidesjQuery
from the global scope (which select2 needs). The filter's__init__
getsModelAdmin
instance passed, however itsMedia
instance cannot be changed at this point. The only option I found is extending the admin'schange_list.html
template, which adds complexity to the module (needs to be inserted beforedjango.contrib.admin
inINSTALLED_APPS
.It works, but its far from being modular.