我希望有默认行为,其中视图中的查询集返回发布日期小于某个日期的记录。我还想要一个布尔 DRF 过滤器,如果设置为 true,则无论 publish_at 日期是什么,它都会返回所有记录。
视图.py:
queryset = ProjectNews.objects.exclude(publish_at__gte=timezone.now())
过滤器.py:
from django_filters import rest_framework as filters
class ProjectNewsFilterSet(filters.FilterSet):
not_published = filters.BooleanFilter(
method='show_not_published_news'
)
def show_not_published(self, queryset, name, value):
if value:
# queryset based on queryset from view
# does not return all the records
return queryset.all()
return queryset.exclude(publish_at__gte=timezone.now())