{"id":13468501,"url":"https://github.com/farhan0581/django-admin-autocomplete-filter","last_synced_at":"2025-05-15T20:03:29.045Z","repository":{"id":37432294,"uuid":"157528622","full_name":"farhan0581/django-admin-autocomplete-filter","owner":"farhan0581","description":"A simple Django app to render list filters in django admin using autocomplete widget. ","archived":false,"fork":false,"pushed_at":"2024-04-16T14:04:03.000Z","size":130,"stargazers_count":356,"open_issues_count":41,"forks_count":79,"subscribers_count":11,"default_branch":"pre_release","last_synced_at":"2025-05-08T04:38:39.168Z","etag":null,"topics":["django","django-admin"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/farhan0581.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-11-14T10:03:11.000Z","updated_at":"2025-04-22T08:52:06.000Z","dependencies_parsed_at":"2024-06-18T15:18:38.255Z","dependency_job_id":"ee796c8e-01f6-4bda-b861-476d01f36514","html_url":"https://github.com/farhan0581/django-admin-autocomplete-filter","commit_stats":{"total_commits":66,"total_committers":20,"mean_commits":3.3,"dds":0.7272727272727273,"last_synced_commit":"0464340f7a8917afce37e80f5078e3c93a68e942"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farhan0581%2Fdjango-admin-autocomplete-filter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farhan0581%2Fdjango-admin-autocomplete-filter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farhan0581%2Fdjango-admin-autocomplete-filter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farhan0581%2Fdjango-admin-autocomplete-filter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/farhan0581","download_url":"https://codeload.github.com/farhan0581/django-admin-autocomplete-filter/tar.gz/refs/heads/pre_release","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254414493,"owners_count":22067271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["django","django-admin"],"created_at":"2024-07-31T15:01:12.344Z","updated_at":"2025-05-15T20:03:27.240Z","avatar_url":"https://github.com/farhan0581.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"[![PyPI version](https://badge.fury.io/py/django-admin-autocomplete-filter.svg?kill_cache=1)](https://badge.fury.io/py/django-admin-autocomplete-filter)\n\n\nDjango Admin Autocomplete Filter\n================================\nA simple Django app to render list filters in django admin using an autocomplete widget. This app is heavily inspired by [dal-admin-filters.](https://github.com/shamanu4/dal_admin_filters)\n\n\nOverview:\n---------\n\nDjango comes preshipped with an admin panel which is a great utility to create quick CRUD's.\nVersion 2.0 came with a much needed [`autocomplete_fields`](https://docs.djangoproject.com/en/2.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields \"autocomplete_fields\") property which uses a select2 widget to load the options asynchronously.  We leverage this in `django-admin-list-filter`.\n\n    \n\nRequirements:\n-------------\n\nRequires Django version \u003e= 2.0\n\n\nFeatures:\n-------------\n\n* Custom search view/endpoint ([more details](#functionality-to-provide-custom-view-for-search))\n* `list_filter` Filter Factory support ([more details](#shortcut-for-creating-filters))\n* Custom widget text ([more details](#customizing-widget-text))\n* Support for [Grappelli](https://grappelliproject.com/)\n\n\nInstallation:\n-------------\n\nYou can install it via pip.  To get the latest version clone this repo.\n\n```shell script\npip install django-admin-autocomplete-filter\n```\n\nAdd `admin_auto_filters` to your `INSTALLED_APPS` inside settings.py of your project.\n\n\nUsage:\n------\n\nLet's say we have following models:\n```python\nfrom django.db import models\n\nclass Artist(models.Model):\n    name = models.CharField(max_length=128)\n\nclass Album(models.Model):\n    name = models.CharField(max_length=64)\n    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)\n    cover = models.CharField(max_length=256, null=True, default=None)\n```\n\nAnd you would like to filter results in `AlbumAdmin` on the basis of `artist`.  You need to define `search fields` in `Artist` and then define filter like this:\n\n```python\nfrom django.contrib import admin\nfrom admin_auto_filters.filters import AutocompleteFilter\n\n\nclass ArtistFilter(AutocompleteFilter):\n    title = 'Artist' # display title\n    field_name = 'artist' # name of the foreign key field\n\n\nclass ArtistAdmin(admin.ModelAdmin):\n    search_fields = ['name'] # this is required for django's autocomplete functionality\n    # ...\n\n\nclass AlbumAdmin(admin.ModelAdmin):\n    list_filter = [ArtistFilter]\n    # ...\n```\n\nAfter following these steps you may see the filter as:\n\n![](https://raw.githubusercontent.com/farhan0581/django-admin-autocomplete-filter/master/admin_auto_filters/media/screenshot1.png)\n\n![](https://raw.githubusercontent.com/farhan0581/django-admin-autocomplete-filter/master/admin_auto_filters/media/screenshot2.png)\n\n\nFunctionality to provide a custom view for search:\n--------------------------------------------------\n\nYou can also register your custom view instead of using Django admin's `search_results` to control the results in the autocomplete. For this you will need to create your custom view and register the URL in your admin class as shown below:\n\nIn your `views.py`:\n\n```python\nfrom admin_auto_filters.views import AutocompleteJsonView\n\n\nclass CustomSearchView(AutocompleteJsonView):\n    def get_queryset(self):\n        \"\"\"\n           your custom logic goes here.\n        \"\"\"\n        queryset = super().get_queryset()\n        queryset = queryset.order_by('name')\n        return queryset\n```\n\nAfter this, register this view in your admin class:\n\n```python\nfrom django.contrib import admin\nfrom django.urls import path\n\n\nclass AlbumAdmin(admin.ModelAdmin):\n    list_filter = [ArtistFilter]\n\n    def get_urls(self):\n        urls = super().get_urls()\n        custom_urls = [\n            path('custom_search/', self.admin_site.admin_view(CustomSearchView.as_view(model_admin=self)),\n                 name='custom_search'),\n        ]\n        return custom_urls + urls\n```\n\nFinally, just tell the filter class to use this new view:\n\n```python\nfrom django.shortcuts import reverse\nfrom admin_auto_filters.filters import AutocompleteFilter\n\n\nclass ArtistFilter(AutocompleteFilter):\n    title = 'Artist'\n    field_name = 'artist'\n\n    def get_autocomplete_url(self, request, model_admin):\n        return reverse('admin:custom_search')\n```\n\n\nShortcut for creating filters:\n------------------------------\n\nIt's also possible to use the `AutocompleteFilterFactory` shortcut to create\nfilters on the fly, as shown below. Nested relations are supported too, with\nno need to specify the model.\n\n```python\nfrom django.contrib import admin\nfrom admin_auto_filters.filters import AutocompleteFilterFactory\n\n\nclass AlbumAdmin(admin.ModelAdmin):\n    list_filter = [\n        AutocompleteFilterFactory('Artist', 'artist', 'admin:custom_search', True)\n    ]\n\n    def get_urls(self):\n        \"\"\"As above...\"\"\"\n```\n\n\nCustomizing widget text\n-----------------------\n\nYou can customize the text displayed in the filter widget, to use something\nother than `str(obj)`. This needs to be configured for both the dropdown\nendpoint and the widget itself.\n\nIn your `views.py`, override `display_text`:\n\n```python\nfrom admin_auto_filters.views import AutocompleteJsonView\n\n\nclass CustomSearchView(AutocompleteJsonView):\n\n    @staticmethod\n    def display_text(obj):\n        return obj.my_str_method()\n\n    def get_queryset(self):\n        \"\"\"As above...\"\"\"\n```\n\nThen use either of two options to customize the text.\n\nOption one is to specify the form_field in an AutocompleteFilter in your\n`admin.py`:\n\n```python\nfrom django import forms\nfrom django.contrib import admin\nfrom django.shortcuts import reverse\nfrom admin_auto_filters.filters import AutocompleteFilter\n\n\nclass FoodChoiceField(forms.ModelChoiceField):\n    def label_from_instance(self, obj):\n        return obj.my_str_method()\n\n\nclass ArtistFilter(AutocompleteFilter):\n    title = 'Artist'\n    field_name = 'artist'\n    form_field = FoodChoiceField\n\n    def get_autocomplete_url(self, request, model_admin):\n        return reverse('admin:custom_search')\n\n\nclass AlbumAdmin(admin.ModelAdmin):\n    list_filter = [ArtistFilter]\n\n    def get_urls(self):\n        \"\"\"As above...\"\"\"\n```\n\nOption two is to use an AutocompleteFilterFactory in your `admin.py`\nadd a `label_by` argument:\n\n```python\nfrom django.contrib import admin\nfrom admin_auto_filters.filters import AutocompleteFilterFactory\n\n\nclass AlbumAdmin(admin.ModelAdmin):\n    list_filter = [\n        AutocompleteFilterFactory('Artist', 'artist', 'admin:custom_search', True, label_by='my_str_method')\n    ]\n\n    def get_urls(self):\n        \"\"\"As above...\"\"\"\n```\n\n\nContributing:\n------------\n\nThis project is a combined effort of a lot of selfless developers who try to make things easier. Your contribution is most welcome.\n\nPlease make a pull-request to the branch `pre_release`, make sure your branch does not have any conflicts, and clearly mention the problems or improvements your PR is addressing.\n\n\nLicense:\n--------\n\nDjango Admin Autocomplete Filter is an Open Source project licensed under the terms of the GNU GENERAL PUBLIC LICENSE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarhan0581%2Fdjango-admin-autocomplete-filter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffarhan0581%2Fdjango-admin-autocomplete-filter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarhan0581%2Fdjango-admin-autocomplete-filter/lists"}