我想从过滤器的视图中获取parent_project。 我有工作代码。 我从以下位置获取parent_project:request.parser_context ['kwargs'] ['parent_project'] 我认为这不是正确的解决方案。有更好的方法吗?
views.py:
class ProjectNewsViewSet(HitCountMixin, viewsets.ModelViewSet):
...
def list(self, request, parent_project, *args, **kwargs):
self.queryset = self.get_queryset().filter(project_id=parent_project)
return super().list(request, parent_project, *args, **kwargs)
filters.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_news(self, queryset, name, value):
if value:
return ProjectNews.objects.filter(
project=self.request.parser_context['kwargs']['parent_project'])
return queryset