Package: django-admin-autocomplete-all
3 different things in Django Admin:Use select2 (autocomplete_fields) everywhere (because implicit is better than explicit).Get more context for filtering in get_search_results; allows relative easy filtering of popup optionsHide danger delete/edit buttons near to the ForeignKey popupsImportant noteThere are many ways how to include autocomplete support for ForeignKeys in Django.Here we have a manual way, which probably is not good way to go, but we can understand how the things could work internally:https://simpleisbetterthancomplex.com/tutorial/2018/01/29/how-to-implement-dependent-or-chained-dropdown-list-with-django.htmlThe robust industrial and standard way (I think so) is the usage of django-autocomplete-light. The package can be used outside of and inside in the django Admin.
This requires lot of clones of very similar code: 1) in urls.py to attach all ajax entrypoints,
2) in views.py lot of ajax views based on autocomplete.Select2QuerySetView with get_queryset() method which handles the seeked string and optionally the from client forwarded filtering values,
3) the forms which attach the autocomplete.ModelSelect2 widgets,
4) inside the admin.py the attaching of such forms to ModelAdmin or Inlines.It is (django-autocomplete-light) clean and easy solution. However with 20 models in db schema previous can be a difficult work for many hours where you can make lot of mistakes.
This is probably one area where django-admin-autocomplete-all can be easier and faster to implement:
Just turn it on (see Usage) and./manage.py checkwill write where you need add a ModelAdmin (or alternativelly HiddenAdmin) and define its get_search_results (or get_search_results_ajax) method.So it is one way how to go: inside Admin django-admin-autocomplete-all and outside of Admin django-autocomplete-light.
However after some tweaking I see the idea of Django 2+ autocomplete_fields (used by django-admin-autocomplete-all) as pure implemented.
So I will personally prefer use of the great django-autocomplete-light package everywhere.
Thats why you cannot wait big improvements of this package in the future.
But I think with Py 3.9, Dj 3.1 it simple works.DocumentationThe full documentation is athttps://django-admin-autocomplete-all.readthedocs.io.QuickstartInstall django-admin-autocomplete-all:pip install django-admin-autocomplete-allAddautocomplete_allintoINSTALLED_APPS, then collectstatic (both not required if you don’t want enhanced get_search_results filtering.)FeaturesAdd autocomplete_fields for all foreign keys.No need to change INSTALLED_APPS to achieve this.
In your admin.py doimport autocomplete_all as admin.importautocomplete_allasadminclassMyModelAdmin(admin.ModelAdmin):....Alternatively import ModelAdmin, StackedInline and/or TabularInline ‘from autocomplete_all’ instead of ‘from admin’.importautocomplete_allclassMyModelAdmin(autocomplete_all.ModelAdmin):....You will then need implement lot of search_fields=.. settings in related ModelAdmins.
You can try start (ie. runserver) without addingsearch_fieldsand Django will show you what is required.Get more context in get_search_results for additional dynamic filtering.Standard Djangoautocomplete_fieldscannot insideget_search_resultsdistinguish between the ForeignKey which asks for the queryset,
especially if 2 ForeignKey’s from single model target into same model (often example: ForeignKey into User model).
If you add this package (‘autocomplete_all’) into INSTALLED_APPS, then ?key=… will be added into url.
Insideget_search_resultsyou will have access to: application, model, ForeignKey.
See example instatic/autocomplete_all/js/autocomplete_all.js.You need implement filtering into get_search_results of target ModelAdmin (you can use HiddenAdmin class instead).
Instead of get_search_results you can use get_search_results_ajax which run for the autocomplete/ access only.You can also implement dynamic filters based on current value of other form fields.
See Usage for details or read in source code:autocomplete_all/js/autocomplete_all.jsandautocomplete_all.py: ModelAdmin,get_search_results_ajax.Hide danger buttons in Admin ChangeForm.The edit & delete buttons near the ForeignKey have very difficult and danger logic what they will do.
If you addautocomplete_allinINSTALLED_APPSbeforedjango.contrib.admin(or some application which replaces admin design, like django-baton),
then the danger buttons will disapear. Place theautocomplete_all“lower” in list if you don’t want this effect.Running TestsDoes the code actually work? /N/A while we haven’t the 1st test yet./source <YOURVIRTUALENV>/bin/activate
(myenv) $ pip install -r requirements_test.txt
(myenv) $ toxCreditsTools used in rendering this package:cookiecuttercookiecutter-djangopackageHistory0.6.6 (2022-02-16)Solved HttpRequest.is_ajax() deprecation (Dj 3.1) - thx Darío Fragas for help and contribution0.6.3 (2021-03-10)INCOMPATIBLE: MediaMixin renamed to AutocompleteAllMixin (please rename if you have this class in your code)0.6.0 (2021-03-09)
INCOMPATIBLE: autocomplete_params.js renamend to autocomplete_all.js (please rename if you have this in your code)
autocomplete_all.js is now always attached in MediaMixin [later: AutocompleteAllMixin] (for ModelAdmin and both Inlines)
ajax autocomplete call now contains all values from the form (see the urlparams variable): you can easier make filtered popups
++++++++++++++++++0.5.0 (2021-03-05)
HiddenAdmin: allow make related admins for search_fields=.. but hide them for direct access (example: we have them better accessible as Inlines)
++++++++++++++++++0.4.0 (2021-03-04)
INCOMPATIBLE: expand_ajax_location_search func renamed to expand_ajax_params (please rename the function if you have it in your javascript)
wrapper for queryset filtering moved from example (ie. from commented code) to real code; new method .get_search_results_ajax() in ModelAdmin
new documentation in usage.rst
++++++++++++++++++0.3.0 (2021-03-03)if used in INSTALLED_APPS before django.contrib.admin (or admin rewriting app), danger ForeignKey buttons (edit,delete) will disapearimport admin methods (example: .register): in many cases you can justimport autocomplete_all as adminand no more changes in admin.py are needed0.2.6 (2020-05-06)Fix: added class Media to fix some scenario(s) where widget is missing0.2.4 (2020-01-27)gives additional context in get_search_results()Fix: missing .js (in 0.2.0-0.2.3)0.1.6 (2020-01-24)Fix in docs: proper attribute name is: autocomplete_except0.1.4 (2020-01-22)First acceptable version.