本文整理匯總了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