admin.SimpleListFilter not working on SortableAdmin
I'm having some trouble getting the Django Admin's SimpleListFilter to work with SortableAdmin.
I've created a simple models.py and admin.py to demonstrate this.
The error happens when a user clicks on "Past" or "Upcoming" to filter the dates while they are on the "Sortable date" list page.
models.py:
from django.db import models
from adminsortable.models import SortableMixin
class SortableDate(SortableMixin):
date = models.DateField()
order = models.IntegerField(editable=False, db_index=True)
class Meta:
ordering = ('order',)
def __str__(self):
return self.date.strftime("%d-%b-%Y")
class NonSortableDate(models.Model):
date = models.DateField()
def __str__(self):
return self.date.strftime("%d-%b-%Y")
admin.py
from django.contrib import admin
from adminsortable.admin import SortableAdmin
from .models import SortableDate, NonSortableDate
from django.utils import timezone
class ClassOptionDateFilter(admin.SimpleListFilter):
title = 'date'
parameter_name = 'relative_date'
def lookups(self, request, model_admin):
return (
('past', 'Past'),
('upcoming', 'Upcoming'),
)
def queryset(self, request, queryset):
today = timezone.now().date()
if self.value() == 'past':
return queryset.filter(date__lte=today)
if self.value() == 'upcoming':
return queryset.filter(date__gt=today)
@admin.register(SortableDate)
class SortableDateAdmin(SortableAdmin):
# Applying a list filter on this causes this error:
# "FieldError at /admin/core/sortabledate/"
# "Cannot resolve keyword 'is_upcoming' into field. Choices are: date, id, order"
list_filter = (ClassOptionDateFilter,)
@admin.register(NonSortableDate)
class NonSortableDateAdmin(admin.ModelAdmin):
# This works fine because it's in a normal ModelAdmin
list_filter = (ClassOptionDateFilter,)
is SortableAdmin just not meant to be compatible with SimpleListFilter?
Morning @ChrisCrossCrash to be perfectly transparent and honest, I have no idea :)
I've been pretty far removed from any Python or Django development for several years. I'm super slammed, so I don't know when I'll be able to take a look at this. You might be able to get some help on Stack Overflow faster.
@alsoicode No worries! I'm just glad you got back to me so fast. I'll figure something else out for now.
Until then, I'd still love to hear from anybody else if they were able to resolve this issue themselves.
Same issue here @ChrisCrossCrash. How did you end up solving this?
Same issue here @ChrisCrossCrash. How did you end up solving this?
@cchacholiades Unfortunately I didn't. IIRC I just sorted the JSON data in my front end app by date.