Coder Social home page Coder Social logo

uniacco-tech / django-admin-searchable-dropdown Goto Github PK

View Code? Open in Web Editor NEW

This project forked from whoisashish/django-admin-searchable-dropdown

0.0 0.0 1.0 80 KB

A PyPi Project with a Django admin filter implementation that renders as a searchable select field dropdown.

Home Page: https://pypi.org/project/django-admin-searchable-dropdown/

License: MIT License

Python 99.24% HTML 0.71% CSS 0.05%

django-admin-searchable-dropdown's Introduction

PyPI version

Django Admin Searchable Dropdown

A Django admin filter implementation that renders as a searchable select field dropdown.

Overview:

If you have more than twenty values for a field that you want to filter by in Django admin, the filtering sidebar gets long, cluttered, sometimes wide and hence, hard to use.

This app contains the AutocompleteFilter class that renders as a drop-down in the filtering sidebar that can be searched to avoid this problem.

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-searchable-dropdown

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

Usage:

Let's say you have following models:

from django.db import models

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

class CarModel(models.Model):
    name = models.CharField(max_length=64)
    company = models.ForeignKey(CarCompany, on_delete=models.CASCADE)

And you would like to filter results in CarModelAdmin on the basis of company. You need to define search fields in CarCompany and then define filter like this:

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


class CarCompanyFilter(AutocompleteFilter):
    title = 'Company' # display title
    field_name = 'company' # name of the foreign key field


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


class CarModelAdmin(admin.ModelAdmin):
    list_filter = [CarCompanyFilter]
    # ...

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_searchable_dropdown.views import AutocompleteJsonView


class CustomSearchView(AutocompleteJsonView):
    def get_queryset(self):
        """
           your custom logic goes here.
        """
        queryset = CarCompany.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 CarModelAdmin(admin.ModelAdmin):
    list_filter = [CarCompanyFilter]

    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_searchable_dropdown.filters import AutocompleteFilter


class CarCompanyFilter(AutocompleteFilter):
    title = 'Company'
    field_name = 'company'

    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.

        AutocompleteFilterFactory('My title', 'field_name')
        AutocompleteFilterFactory('My title', 'fourth__third__second__first')
    Be sure to include distinct in the model admin get_queryset() if the second form is used.
    Assumes: parameter_name == f'fourth__third__second__{field_name}'
        * title: The title for the filter.
        * base_parameter_name: The field to use for the filter.
        * viewname: The name of the custom AutocompleteJsonView URL to use, if any.
        * use_pk_exact: Whether to use '__pk__exact' in the parameter name when possible.
        * label_by: How to generate the static label for the widget - a callable, the name
          of a model callable, or the name of a model field.```

Example:

```python
from django.contrib import admin
from admin_searchable_dropdown.filters import AutocompleteFilterFactory


class AlbumAdmin(admin.ModelAdmin):
    list_filter = [
        AutocompleteFilterFactory('Company', 'company', '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_searchable_dropdown.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_searchable_dropdown.filters import AutocompleteFilter


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


class CarCompanyFilter(AutocompleteFilter):
    title = 'Company'
    field_name = 'company'
    form_field = FuelChoiceField

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


class CarModelAdmin(admin.ModelAdmin):
    list_filter = [CarCompanyFilter]

    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_searchable_dropdown.filters import AutocompleteFilterFactory


class CarModelAdmin(admin.ModelAdmin):
    list_filter = [
        AutocompleteFilterFactory('Company', 'company', 'admin:custom_search', True, label_by='my_str_method')
    ]

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

Contributing:

Based on this StackOverflow question, and the comments that went unresolved in the selected answer and code from FeinCMS and code from mrts.

To Contribute, please fork the project, make a pull-request, and clearly mention the problems or improvements your PR is addressing.

License:

Django Admin Searchable Dropdown is an Open Source project licensed under the terms of the MIT LICENSE.

django-admin-searchable-dropdown's People

Forkers

manishs6

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.