Custom Filter in Django Admin on Django 1.3 or Below

Custom Filter in Django Admin on Django 1.3 or below

Thanks to gpilotino for giving me the push into the right direction for implementing this.

I noticed the question's code is using a datetime to figure out when its live . So I used the DateFieldFilterSpec and subclassed it.

from django.db import models
from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec,DateFieldFilterSpec
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext as _
from datetime import datetime

class IsLiveFilterSpec(DateFieldFilterSpec):
"""
Adds filtering by future and previous values in the admin
filter sidebar. Set the is_live_filter filter in the model field attribute
'is_live_filter'. my_model_field.is_live_filter = True
"""

def __init__(self, f, request, params, model, model_admin):
super(IsLiveFilterSpec, self).__init__(f, request, params, model,
model_admin)
today = datetime.now()
self.links = (
(_('Any'), {}),
(_('Yes'), {'%s__lte' % self.field.name: str(today),
}),
(_('No'), {'%s__gte' % self.field.name: str(today),
}),

)

def title(self):
return "Is Live"

# registering the filter
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'is_live_filter', False),
IsLiveFilterSpec))

To use you can put the above code into a filters.py, and import it in the model you want to add the filter to

Django admin filter on null date

You'll have to register your own filter - look at Custom Filter in Django Admin on Django 1.3 or below or http://twigstechtips.blogspot.com/2010/10/django-create-custom-admin-model-filter.html or lots of other places for how that's done.

Regarding your particular requirement, a snippet for an "IS NULL/IS NOT NULL" filter at http://djangosnippets.org/snippets/1963/

Django admin filter on edit page

I found the documentation on https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.autocomplete_fields

The code below adds a search box:

class TagAdmin(admin.ModelAdmin):
search_fields = ['name']

class CompanyAdmin(admin.ModelAdmin):
autocomplete_fields = ['tags']

Django admin queryset filtering by foreign key backwards relation

You need to read this: Custom Filter in Django Admin on Django 1.3 or below

This is my first attempt without any testing, but you should see more or less how its done -

from django.db import models
from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec
from django.utils.encoding import smart_unicode
from django.utils.translation import ugettext as _

class BNullSetFilterSpec(FilterSpec):

def __init__(self, f, request, params, model, model_admin):
super(BSetFilterSpec, self).__init__(f, request, params, model, model_admin)

self.links = (
('Yes', {'b__isnull': False}),
('No', {}))

def title(self):
return _('B Set')

# registering the filter
FilterSpec.filter_specs.insert(0, (lambda f: getattr(f, 'empty_bset', False), BNullSetFilterSpec))

How to customize admin filter in Django 1.4

There is new django.contrib.admin.SimpleListFilter introduced in v1.4 meet your need, and official document provide sample code and easy to read.
search SimpleListFilter in this section.

Django admin list filter

See this SO thread. It's not as easy as it feels like it should be.

Can I make list_filter in django admin to only show referenced ForeignKeys?

As of Django 1.8, there is a built in RelatedOnlyFieldListFilter, which you can use to show related countries.

class MyModelAdmin(admin.ModelAdmin):
list_display = ('name', 'country',)
list_filter = (
('country', admin.RelatedOnlyFieldListFilter),
)

For Django 1.4-1.7, list_filter allows you to use a subclass of SimpleListFilter. It should be possible to create a simple list filter that lists the values you want.

If you can't upgrade from Django 1.3, you'd need to use the internal, and undocumented, FilterSpec api. The Stack Overflow question Custom Filter in Django Admin should point you in the right direction.



Related Topics



Leave a reply



Submit