def make_active_productlist_query(queryset):
now = timezone.now()
# Create a query for the set of products that MIGHT be active. Might
# because they can be out of stock. Which we compute later
active_candidates = (
queryset
.filter(
Q(active=True)
& (Q(deactivate_date=None) | Q(deactivate_date__gte=now)))
)
# This query selects all the candidates that are out of stock.
candidates_out_of_stock = (
active_candidates
.filter(sale__timestamp__gt=F("start_date"))
.annotate(c=Count("sale__id"))
.filter(c__gte=F("quantity"))
.values("id")
)
# We can now create a query that selects all the candidates which are not
# out of stock.
return (
active_candidates
.exclude(
Q(start_date__isnull=False)
& Q(id__in=candidates_out_of_stock)))
评论列表
文章目录