Add new filter
This commit is contained in:
parent
b55a8ffda6
commit
a0a6578ae8
|
|
@ -46,7 +46,7 @@ class CollectionHomePageView(CollectionViewMixin, generics.ListAPIView):
|
|||
"""Override get_queryset method"""
|
||||
queryset = models.Collection.objects.published()\
|
||||
.by_country_code(code=self.request.country_code)\
|
||||
.filter_related_gt(3)\
|
||||
.filter_all_related_gt(3)\
|
||||
.order_by('-on_top', '-modified')
|
||||
|
||||
return queryset
|
||||
|
|
|
|||
|
|
@ -1,7 +1,8 @@
|
|||
"""Utils QuerySet Mixins"""
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
|
||||
from django.db.models import Q, Sum, F
|
||||
from functools import reduce
|
||||
from operator import add
|
||||
from utils.methods import get_contenttype
|
||||
|
||||
|
||||
|
|
@ -21,7 +22,7 @@ class ContentTypeQuerySetMixin(models.QuerySet):
|
|||
class RelatedObjectsCountMixin(models.QuerySet):
|
||||
"""QuerySet for ManyToMany related count filter"""
|
||||
|
||||
def get_related_objects_names(self):
|
||||
def _get_related_objects_names(self):
|
||||
"""Get all related objects (with reversed)"""
|
||||
related_objects_names = []
|
||||
|
||||
|
|
@ -31,10 +32,10 @@ class RelatedObjectsCountMixin(models.QuerySet):
|
|||
|
||||
return related_objects_names
|
||||
|
||||
def annotate_related_objects_count(self):
|
||||
def _annotate_related_objects_count(self):
|
||||
"""Annotate all related objects to queryset"""
|
||||
annotations = {}
|
||||
for related_object in self.get_related_objects_names():
|
||||
for related_object in self._get_related_objects_names():
|
||||
annotations[f"{related_object}_count"] = models.Count(f"{related_object}")
|
||||
|
||||
return self.annotate(**annotations)
|
||||
|
|
@ -42,7 +43,13 @@ class RelatedObjectsCountMixin(models.QuerySet):
|
|||
def filter_related_gt(self, count):
|
||||
"""QuerySet filter by related objects count"""
|
||||
q_objects = Q()
|
||||
for related_object in self.get_related_objects_names():
|
||||
for related_object in self._get_related_objects_names():
|
||||
q_objects.add(Q(**{f"{related_object}_count__gt": count}), Q.OR)
|
||||
|
||||
return self.annotate_related_objects_count().filter(q_objects)
|
||||
return self._annotate_related_objects_count().filter(q_objects)
|
||||
|
||||
def filter_all_related_gt(self, count):
|
||||
exp =reduce(add, [F(f"{related_object}_count") for related_object in self._get_related_objects_names()])
|
||||
return self._annotate_related_objects_count()\
|
||||
.annotate(all_related_count=exp)\
|
||||
.filter(all_related_count__gt=count)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user