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

SimpleListFilter #118

Open
valeriozhang opened this issue May 19, 2020 · 3 comments
Open

SimpleListFilter #118

valeriozhang opened this issue May 19, 2020 · 3 comments

Comments

@valeriozhang
Copy link

Is it possible to create a custom list filter for advanced_filter_fields? I have a field property in my model that I would like to search for. Thanks ! :)

@dangerousmanjay
Copy link

dangerousmanjay commented Sep 10, 2020

same here! i am also wondering if i can use this mixin with my custom simple list filter.

@asfaltboy
Copy link
Member

This package is about exposing fields in a form for generating a db query. How do you imagine a "simple filter" of a different type will work alongside?

Can you give me an example of how this would look?

@dangerousmanjay
Copy link

@asfaltboy
Thanks for responding so fast! 👍

here's an example

in my models.py

from django.db import models


class Brain(models.Model):
    is_sexy = models.BooleanField()


class Cell(models.Model):
    brain = models.ForeignKey(Brain, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)

in my admin.py

from datetime import timedelta

from django.contrib import admin
from django.db.models import OuterRef, Subquery
from django.utils.timezone import now


class LatestCellDateTimeFilter(SimpleListFilter):
    title = 'Latest cell created at'

    parameter_name = 'latest_cell_created_at'

    def lookups(self, request, model_admin):
        return (
            ('today', 'today'),
            ('week', 'past one week'),
        )

    def queryset(self, request, queryset):
        if self.value() == 'today':
            return queryset.filter(latest_cell_created_at=now())
        if self.value() == 'week':
            return queryset.filter(
                latest_cell_created_at=now(),
                latest_cell_created_at=now() - timedelta(weeks=1),
            )


@admin.register(Brain)
class BrainAdmin(admin.ModelAdmin):
    list_filter = (
        LatestCellDateTimeFilter,
        'is_sexy',
    )

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        cells = Cell.objects.filter(brain=OuterRef('pk')).order_by('-created_at')
        return qs.annotate(
            latest_cell_created_at=Subquery(cells.values('created_at')[:1]),
        )

In this situation, is there a way to make it work with this package other than just chaining two filters?
I know it won't work since 'latest_cell_created_at' isn't a actual field, and Django ORM doesn't support that kind of lookup. I'm hoping someone find a way to get through haha

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants