All Projects → farhan0581 → Django Admin Autocomplete Filter

farhan0581 / Django Admin Autocomplete Filter

Licence: gpl-3.0
A simple Django app to render list filters in django admin using autocomplete widget.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Django Admin Autocomplete Filter

Django Suit Daterange Filter
Filter for django-admin allowing lookups by date range
Stars: ✭ 13 (-92.17%)
Mutual labels:  django, django-admin
Awesome Django Cn
Django 优秀资源大全。
Stars: ✭ 1,153 (+594.58%)
Mutual labels:  django, django-admin
E Commerce 2 django
Guest register, user register, user login, user logout, account home page, product view history, change password, reset password, change name, send activation email when register, resend activation email, add shipping address, add billing address, add nickname to the addresses, edit shipping address, edit billing address, view list of your addresses, reuse shipping addresses when order products, reuse billing addresses when ordeer products, show sales analytics if staff or admin only using -chart.js-, get analytics data with Ajax, receive marketing email, change if user will receive marketing email or not by admin, send contact message with Ajax, products list, product detail, download product detail as a PDF file, download digital product files -if the user purchased that digital product only-, orders list, list of digital products files, order detail, download order detail as a PDF file, verify order ownership with Ajax -to secure order detail page-, show cart products, add or remove product from cart, checkout page, thanks page when order placed successfully, add or reuse payment method, add or reuse payment method with Ajax, search products by title, search products by description, search products by price, search products by tag title, write tags for products -by admin only-, auto fill contact email, full name if user logged in.
Stars: ✭ 20 (-87.95%)
Mutual labels:  django, django-admin
Django Oml
Object Moderation Layer
Stars: ✭ 12 (-92.77%)
Mutual labels:  django, django-admin
Django Fluent Pages
A flexible, scalable CMS with custom node types, and flexible block content.
Stars: ✭ 103 (-37.95%)
Mutual labels:  django, django-admin
Requery
Store e run queries on database to help system manager of a Django website
Stars: ✭ 12 (-92.77%)
Mutual labels:  django, django-admin
Django Polymorphic
Improved Django model inheritance with automatic downcasting
Stars: ✭ 1,135 (+583.73%)
Mutual labels:  django, django-admin
Awesome Django
The Best Django Resource, Awesome Django for mature packages.
Stars: ✭ 591 (+256.02%)
Mutual labels:  django, django-admin
Django Admin Relation Links
An easy way to add links to relations in the Django Admin site.
Stars: ✭ 87 (-47.59%)
Mutual labels:  django, django-admin
Awesome Django
Repository mirror of GitLab: https://gitlab.com/rosarior/awesome-django This repository is not monitored for issues, use original at GitLab.
Stars: ✭ 8,527 (+5036.75%)
Mutual labels:  django, django-admin
Django Blogging System
This is blog system created using Django 1.11.4 and Python3
Stars: ✭ 11 (-93.37%)
Mutual labels:  django, django-admin
Django Business Logic
Visual DSL framework for django
Stars: ✭ 134 (-19.28%)
Mutual labels:  django, django-admin
Django Phantom Theme
Phantom is theme for django admin with many widgets, based on Twitter bootstrap 3.x.
Stars: ✭ 18 (-89.16%)
Mutual labels:  django, django-admin
Django Material Admin
Material design for django administration
Stars: ✭ 163 (-1.81%)
Mutual labels:  django, django-admin
Django Admin Bootstrap
Responsive Theme for Django Admin With Sidebar Menu
Stars: ✭ 787 (+374.1%)
Mutual labels:  django, django-admin
Django Admin Numeric Filter
Numeric filters for Django admin
Stars: ✭ 46 (-72.29%)
Mutual labels:  django, django-admin
Django Ordered Model
Get your Django models in order
Stars: ✭ 476 (+186.75%)
Mutual labels:  django, django-admin
Django Pagedown
A django app that allows the easy addition of Stack Overflow's "PageDown" markdown editor to a django form field, whether in a custom app or the Django Admin
Stars: ✭ 500 (+201.2%)
Mutual labels:  django, django-admin
Django Admin Material
A Django Admin interface based on Material Design by Google
Stars: ✭ 74 (-55.42%)
Mutual labels:  django, django-admin
Django Subadmin
A special kind of ModelAdmin that allows it to be nested within another ModelAdmin
Stars: ✭ 120 (-27.71%)
Mutual labels:  django, django-admin

PyPI version

Django Admin Autocomplete Filter

A simple Django app to render list filters in django admin using an autocomplete widget. This app is heavily inspired by dal-admin-filters.

Overview:

Django comes preshipped with an admin panel which is a great utility to create quick CRUD's. Version 2.0 came with a much needed autocomplete_fields property which uses a select2 widget to load the options asynchronously. We leverage this in django-admin-list-filter.

Requirements:

Requires Django version >= 2.0

Features:

Installation:

You can install it via pip. To get the latest version clone this repo.

pip install django-admin-autocomplete-filter

Add admin_auto_filters to your INSTALLED_APPS inside settings.py of your project.

Usage:

Let's say we have following models:

from django.db import models

class Artist(models.Model):
    name = models.CharField(max_length=128)

class Album(models.Model):
    name = models.CharField(max_length=64)
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
    cover = models.CharField(max_length=256, null=True, default=None)

And 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:

from django.contrib import admin
from admin_auto_filters.filters import AutocompleteFilter


class ArtistFilter(AutocompleteFilter):
    title = 'Artist' # display title
    field_name = 'artist' # name of the foreign key field


class ArtistAdmin(admin.ModelAdmin):
    search_fields = ['name'] # this is required for django's autocomplete functionality
    # ...


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [ArtistFilter]
    # ...

After following these steps you may see the filter as:

Functionality to provide a custom view for search:

You 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:

In your views.py:

from admin_auto_filters.views import AutocompleteJsonView


class CustomSearchView(AutocompleteJsonView):
    def get_queryset(self):
        """
           your custom logic goes here.
        """
        queryset = Artist.objects.all().order_by('name')
        return queryset

After this, register this view in your admin class:

from django.contrib import admin
from django.urls import path


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [ArtistFilter]

    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            path('custom_search/', self.admin_site.admin_view(CustomSearchView.as_view(model_admin=self)),
                 name='custom_search'),
        ]
        return custom_urls + urls

Finally, just tell the filter class to use this new view:

from django.shortcuts import reverse
from admin_auto_filters.filters import AutocompleteFilter


class ArtistFilter(AutocompleteFilter):
    title = 'Artist'
    field_name = 'artist'

    def get_autocomplete_url(self, request, model_admin):
        return reverse('admin:custom_search')

Shortcut for creating filters:

It's also possible to use the AutocompleteFilterFactory shortcut to create filters on the fly, as shown below. Nested relations are supported too, with no need to specify the model.

from django.contrib import admin
from admin_auto_filters.filters import AutocompleteFilterFactory


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [
        AutocompleteFilterFactory('Artist', 'artist', 'admin:custom_search', True)
    ]

    def get_urls(self):
        """As above..."""

Customizing widget text

You can customize the text displayed in the filter widget, to use something other than str(obj). This needs to be configured for both the dropdown endpoint and the widget itself.

In your views.py, override display_text:

from admin_auto_filters.views import AutocompleteJsonView


class CustomSearchView(AutocompleteJsonView):

    @staticmethod
    def display_text(obj):
        return obj.my_str_method()

    def get_queryset(self):
        """As above..."""

Then use either of two options to customize the text.

Option one is to specify the form_field in an AutocompleteFilter in your admin.py:

from django import forms
from django.contrib import admin
from django.shortcuts import reverse
from admin_auto_filters.filters import AutocompleteFilter


class FoodChoiceField(forms.ModelChoiceField):
    def label_from_instance(self, obj):
        return obj.my_str_method()


class ArtistFilter(AutocompleteFilter):
    title = 'Artist'
    field_name = 'artist'
    form_field = FoodChoiceField

    def get_autocomplete_url(self, request, model_admin):
        return reverse('admin:custom_search')


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [ArtistFilter]

    def get_urls(self):
        """As above..."""

Option two is to use an AutocompleteFilterFactory in your admin.py add a label_by argument:

from django.contrib import admin
from admin_auto_filters.filters import AutocompleteFilterFactory


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [
        AutocompleteFilterFactory('Artist', 'artist', 'admin:custom_search', True, label_by='my_str_method')
    ]

    def get_urls(self):
        """As above..."""

Contributing:

This project is a combined effort of a lot of selfless developers who try to make things easier. Your contribution is most welcome.

Please 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.

License:

Django Admin Autocomplete Filter is an Open Source project licensed under the terms of the GNU GENERAL PUBLIC LICENSE.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].