Package: django-admin-model-filters
django-admin-model-filtersA Django extension that allows filtering models in the changelist view of
the admin interface using the model's individual fields.FeaturesFilter models by specific fields and types, instead of a generic search that
converts everything toUPPERstrings. This allows the native data types
to be used, and any indexes you have to be taken advantage of.Supports object level permissions withdjango-guardian. Model filters can
be shared with users and/or groups through the admin interface. Ifdjango-guardianis not setup, regular Django class level permissions are
used, unless theMODEL_FILTERS_*_OWNER_ONLYsettings areTrue.Supportsgrappelliadmin skin.InstallationInstall the extension into your environment:pipinstalldjango-admin-model-filtersAdd the extension to the list ofINSTALLED_APPS:INSTALLED_APPS=[..."model_filters",...]Run the migrations for the extension:./manage.pymigratemodel_filtersUsageAdd theModelFilterMixinto any existingModelAdminclasses in
your project that you wish to filter.Define a list or tuple of fields,model_filters, that you want to filter the
model using. The values should be of one of the following types:A field name. Field names can also span relations using the__lookup. By
default, the field'sverbose_nameis used for display purposes.A tuple, where the first element is a field name, and the second element is
a custom verbose name to display for the field.For example:fromdjango.contribimportadminfrommodel_filters.admin.mixinsimportModelFilterMixin@admin.register(Widget)classWidgetModelAdmin(ModelFilterMixin,admin.ModelAdmin):...model_filters=[("name","Widget Name"),"part_number",("component__name","Component Name"),"serial_number",]...PermissionsUses Django's built-in permission framework, and supportsdjango-guardianfor
object-level permissions.By default, model filters can only be used by the user that created them (theirowner). Use theMODEL_FILTERS_*_OWNER_ONLYsettings to allow sharing them
with other users and groups.Note that you can of course override any of thehas_*_permissionmethods
by creating your ownModelAdminthat extends and replaces the existingModelFilterAdmin.SettingsThe following settings can be configured in yoursettings.py:MODEL_FILTERS_VIEW_OWNER_ONLYDefault:TrueIfTrue, only theownerof a model filter can view it, regardless of any
permissions that have been set. IfFalse, any user or group with theviewpermission can view the model filter. Superusers have full access.MODEL_FILTERS_CHANGE_OWNER_ONLYDefault:TrueIfTrue, only theownerof a model filter can change it, regardless of any
permissions that have been set. IfFalse, any user or group with thechangepermission can change the model filter. Superusers have full access.MODEL_FILTERS_DELETE_OWNER_ONLYDefault:TrueIfTrue, only theownerof a model filter can delete it, regardless of any
permissions that have been set. IfFalse, any user or group with thedeletepermission can delete the model filter. Superusers have full access.MODEL_FILTERS_ORDER_BYDefault:[Lower("name").asc(nulls_first=True), "-created"]A field, or list of fields, to order the model filters by in the model admin
list filter menu. The contents of this setting will be passed directly to theorder_bymethod of the queryset.MODEL_FILTERS_USE_GUARDIANDefault:TrueIfTrue, andguardianis found in yourINSTALLED_APPS, then object level
permissions will be used via Django Guardian. Note that object level permissions
are only available ifMODEL_FILTERS_*_OWNER_ONLYareFalse.DevelopersCreate a Python 3.7 virtualenv and install the developer requirements into it:pipinstall-rrequirements/developer.txtThis is atoxbased project. Runtoxto ensure you are in a good state.
You may want to edittox.inito exclude specific Python or Django versions
that your environment does not support, or just to simplify the build during
development.Create your local developer settings:cpacme/settings/developer.pyacme/settings/local_settings.pyUse this settings file to override any settings during development. This file
is ignored by git and the packager.Development and Testing ProjectThe core extension is in themodel_filterspackage.Theacmepackage is a project used for development and testing purposes.
Use the./manage.pyfile at the project root like you would any other Django
project, including./manage.py runserverto explore theacmetest project.Some sample data is provided inacme/fixtures/data.json. Load it into your
database with./manage.py loaddata acme/fixtures/data.json.Code StyleThis project usesblackandisortto maintain consistency and uniformity.Runtox -e codestyleto check your code style. Runblack .andisort .to format the entire project, or configure your IDE to run them after saving.Code AnalysisThis project usesflake8andpylintto find bugs and maintain quality.Runtox -e flake8andtox -e pylintto analyse your code style.TestingThis project usespytestandcoverage.Look atpytest.inito see the available pytest markers and overall pytest
configuration.Check your coverage after running tests here:build/reports/htmlcov/index.htmlBasicsRun all tests with Python 3.7 and Django 3.2:tox-epy37-django32Run all end-to-end tests:tox-epy37-django32---me2eRun everything except the end-to-end tests:tox-epy37-django32---m"not e2e"Run all tests in a specific test class:tox-epy37-django32--acme/tests/e2e/modelfilter_create_tests.py::TestsRun a specific test in a test class:tox-epy37-django32--model_filters/utilities_tests.py::Tests::test_user_has_permission