django admin: use select2 (autocomplete_fields) everywhere
Project description
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 options
Hide danger delete/edit buttons near to the ForeignKey popups
Documentation
The full documentation is at https://django-admin-autocomplete-all.readthedocs.io.
Quickstart
Install django-admin-autocomplete-all:
pip install django-admin-autocomplete-all
Add autocomplete_all into INSTALLED_APPS, then collectstatic (both not required if you don’t want enhanced get_search_results filtering.)
Features
Use select2 (autocomplete_fields) everywhere.
No need to change INSTALLED_APPS to achieve this. In your admin.py do import autocomplete_all as admin.
import autocomplete_all as admin
class MyModelAdmin(admin.ModelAdmin):
....
Alternatively import ModelAdmin, StackedInline and/or TabularInline ‘from autocomplete_all’ instead of ‘from admin’.
import autocomplete_all
class MyModelAdmin(autocomplete_all.ModelAdmin):
....
You will then need implement lot of search_fields=.. settings in related ModelAdmins. You can try start (ie. runserver) without adding search_fields and Django will show you what is required.
Get more context in get_search_results.
Standard Django autocomplete_fields cannot inside get_search_results distinguish 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. Inside get_search_results you will have access to: application, model, ForeignKey. See example in static/autocomplete_all/js/autocomplete_all.js.
Implement filtering into get_search_results of target ModelAdmin and add this to the source ModelAdmin:
class MyModelAdmin(ModelAdmin): # ModelAdmin can be standard or autocomplete_all.ModelAdmin
class Media:
js = ('autocomplete_all/js/autocomplete_all.js',)
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.js and autocomplete_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 add autocomplete_all in INSTALLED_APPS before django.contrib.admin (or some application which replaces admin design, like django-baton), then the danger buttons will disapear. Place the autocomplete_all “lower” in list if you don’t want this effect.
Running Tests
Does 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) $ tox
Credits
Tools used in rendering this package:
History
0.6.1 (2021-03-10)
docs only (dependent popups)
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 (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 disapear
import admin methods (example: .register): in many cases you can just import autocomplete_all as admin and no more changes in admin.py are needed
0.2.6 (2020-05-06)
Fix: added class Media to fix some scenario(s) where widget is missing
0.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_except
0.1.4 (2020-01-22)
First acceptable version.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-admin-autocomplete-all-0.6.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 21670d1c5c66b54bbcc56bea0ee2bc1c58cdac4e086dc61400311cf1e432316c |
|
MD5 | 00ffa52e141b520a705a4ecd33dc3efc |
|
BLAKE2b-256 | 0d57682e04ad0772e1ed185aba6e2d0403e57ae6c7415dd0c34b5aa1b11f2be6 |
Hashes for django_admin_autocomplete_all-0.6.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c36310e36af678826043c63caa07a33b423d119a69452866a0407df305a42a85 |
|
MD5 | 68af4f57eaa677c1ef5a1e985d33ce4c |
|
BLAKE2b-256 | 9382f4b0a3e659c3660fa69773563107f4a697b9bba65278039cef4ab5c8fc11 |