Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admin.SimpleListFilter not working on SortableAdmin #242

Open
ChrisCrossCrash opened this issue Apr 8, 2021 · 4 comments
Open

admin.SimpleListFilter not working on SortableAdmin #242

ChrisCrossCrash opened this issue Apr 8, 2021 · 4 comments

Comments

@ChrisCrossCrash
Copy link

ChrisCrossCrash commented Apr 8, 2021

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?

@alsoicode
Copy link
Collaborator

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.

@ChrisCrossCrash
Copy link
Author

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

@cchacholiades
Copy link

Same issue here @ChrisCrossCrash. How did you end up solving this?

@ChrisCrossCrash
Copy link
Author

ChrisCrossCrash commented Aug 18, 2022

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.

@dntstp dntstp mentioned this issue Apr 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants