queryset = queryset.annotate(
row_number=RawSQL('row_number() OVER (ORDER BY поле_сортировки)', [])
).annotate(rowmod5=F('row_number') % 5).filter(rowmod5=0)
class DateTimeGteFilter(Filter):
def filter(qs, value):
return qs.prefetch_related(
Prefetch(
'coincost_set',
to_attr='filtered_costs',
queryset=CoinCost.objects.filter(timestamp__gte=value)
)
)
class CoinCostFilterSet(FilterSet):
timestamp_start = DateTimeGteFilter()
...
class CoinSerializer(serializers.ModelSerializer):
class Meta:
model = models.Coin
costs = CoinCostSerializer(source='coincost_set.all', many=True)