-
Notifications
You must be signed in to change notification settings - Fork 176
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
Comments
same here! i am also wondering if i can use this mixin with my custom simple list filter. |
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? |
@asfaltboy 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? |
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 ! :)
The text was updated successfully, but these errors were encountered: