本文整理汇总了Python中django_filters.rest_framework.Filter方法的典型用法代码示例。如果您正苦于以下问题:Python rest_framework.Filter方法的具体用法?Python rest_framework.Filter怎么用?Python rest_framework.Filter使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django_filters.rest_framework
的用法示例。
在下文中一共展示了rest_framework.Filter方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: filter_owners
# 需要导入模块: from django_filters import rest_framework [as 别名]
# 或者: from django_filters.rest_framework import Filter [as 别名]
def filter_owners(self, queryset, name, value):
"""Filter queryset by owner's id."""
try:
user = user_model.objects.get(pk=value)
except user_model.DoesNotExist:
return queryset.none()
return get_objects_for_user(
user, self.owner_permission, queryset, with_superuser=False
)
示例2: filter_owners_name
# 需要导入模块: from django_filters import rest_framework [as 别名]
# 或者: from django_filters.rest_framework import Filter [as 别名]
def filter_owners_name(self, queryset, name, value):
"""Filter queryset by owner's name."""
result = queryset.model.objects.none()
user_subquery = self._get_user_subquery(value)
for user in user_subquery:
result = result.union(
get_objects_for_user(
user, self.owner_permission, queryset, with_superuser=False
)
)
# Union can no longer be filtered, so we have to create a new queryset
# for following filters.
return result.model.objects.filter(pk__in=Subquery(result.values("pk")))
示例3: filter_contributor_name
# 需要导入模块: from django_filters import rest_framework [as 别名]
# 或者: from django_filters.rest_framework import Filter [as 别名]
def filter_contributor_name(self, queryset, name, value):
"""Filter queryset by owner's name."""
return queryset.filter(contributor__in=self._get_user_subquery(value))
示例4: filter
# 需要导入模块: from django_filters import rest_framework [as 别名]
# 或者: from django_filters.rest_framework import Filter [as 别名]
def filter(self, qs, value):
"""Filter for given most frequently used tasks.
Most frequently used tasks are only counted within last
few months as older tasks are not relevant anymore
for today's usage.
:param QuerySet qs: The queryset to filter
:param int value: number of most frequent items
:return: The filtered queryset
:rtype: QuerySet
"""
if value in EMPTY_VALUES:
return qs
user = self.parent.request.user
from_date = date.today() - timedelta(days=60)
qs = qs.filter(
reports__user=user,
reports__date__gt=from_date,
archived=False,
project__archived=False,
)
qs = qs.annotate(frequency=Count("reports")).order_by("-frequency")
# limit number of results to given value
qs = qs[: int(value)]
return qs