Giter Site home page Giter Site logo

jarrebylove / dal_admin_filters Goto Github PK

View Code? Open in Web Editor NEW

This project forked from shamanu4/dal_admin_filters

0.0 0.0 0.0 87 KB

Django autocomplete light filters for django admin

License: MIT License

Python 78.69% CSS 0.42% JavaScript 16.53% HTML 4.36%

dal_admin_filters's Introduction

dal_admin_filters

Django-autocomplete-light filters for django admin.

default select2 widget

alt text

widget with title as placeholder

alt text

Requirements

This is extension for django-autocomplete-light so you need to install and configure it too.

Here will be minimum setup for example.

Refer to http://django-autocomplete-light.readthedocs.io/ for more detailed instructions.

Installation

  • Install using pip

    pip install django-autocomplete-light dal_admin_filters
    
  • Update INSTALLED_APPS, you need too put django-autocomplete-light before admin

    INSTALLED_APPS = [
        'dal',
        'dal_select2',
        'dal_admin_filters',
        #
        'django.contrib.admin',
        ... other stuff there ...
        ]

Configuration

  • Create autocomplete view
    • Let our models look like this
      class Country(models.Model):
          name = models.CharField(max_length=100, unique=True)
      
          def __str__(self):
              return self.name
      
      
      class Person(models.Model):
          name = models.CharField(max_length=100, unique=True)
          from_country = models.ForeignKey(Country)
      
          def __str__(self):
              return self.name
    • Then autocomplete view for country selection will be similar to next
      from dal import autocomplete
      from your_countries_app.models import Country
      
      class CountryAutocomplete(autocomplete.Select2QuerySetView):
          def get_queryset(self):
              # Don't forget to filter out results depending on the visitor !
              if not self.request.user.is_authenticated():
                  return Country.objects.none()
          
              qs = Country.objects.all()
          
              if self.q:
                  qs = qs.filter(name__istartswith=self.q)
          
              return qs
  • Register view in urls.py
    from your_countries_app.views import CountryAutocomplete
    
    urlpatterns = [
        url(
            r'^country-autocomplete/$',
            CountryAutocomplete.as_view(),
            name='country-autocomplete',
        ),
        url(r'^admin/', admin.site.urls),
    ]
  • Use filter in your admin.py
    from django.contrib import admin
    from your_countries_app.models import Country, Person
    from dal_admin_filters import AutocompleteFilter
    
    
    @admin.register(Country)
    class CountryAdmin(admin.ModelAdmin):
        pass
    
    
    class CountryFilter(AutocompleteFilter):
        title = 'Country from'                    # filter's title
        field_name = 'from_country'           # field name - ForeignKey to Country model
        autocomplete_url = 'country-autocomplete' # url name of Country autocomplete view
    
    
    class CountryPlaceholderFilter(AutocompleteFilter):
        title = 'Country from'                    # filter's title
        field_name = 'from_country'           # field name - ForeignKey to Country model
        autocomplete_url = 'country-autocomplete' # url name of Country autocomplete view
        is_placeholder_title = True               # filter title will be shown as placeholder
    
    
    class CountryCustomPlaceholderFilter(AutocompleteFilter):
        title = 'Country from'                    # filter's title
        parameter_name = 'from_country'           # field name - ForeignKey to Country model
        autocomplete_url = 'country-autocomplete' # url name of Country autocomplete view
        widget_attrs = {
            'data-placeholder': 'Filter by country name'
        }
    
    
    @admin.register(Person)
    class PersonAdmin(admin.ModelAdmin):
        class Media:    # Empty media class is required if you are using autocomplete filter
            pass        # If you know better solution for altering admin.media from filter instance
                        #   - please contact me or make a pull request
          
        list_filter = [CountryFilter]

If setup is done right, you will see the Select2 widget in admin filter in Person's changelist view.

dal_admin_filters's People

Contributors

shamanu4 avatar snnwolf avatar belonesox avatar saladrai avatar xrmx avatar ron8mcr avatar lunarabbit avatar maks3w avatar maximzemskov avatar jarrebylove avatar

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.