Skip to content

Commit

Permalink
Fix after filter when ordering on annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
daanvdk committed May 23, 2024
1 parent 3388534 commit daff032
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions binder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1543,20 +1543,35 @@ def _apply_q_with_possible_annotations(self, queryset, q, annotations):
return queryset.filter(q)


def _after_expr(self, request, after_id):
def _after_expr(self, request, after_id, include_annotations):
"""
This method given a request and an id returns a boolean expression that
indicates if a record would show up after the provided id for the
ordering specified by this request.
"""
# First we get the object we need to use as our base for our filter
queryset = self.get_queryset(request)
annotations = {
name: value['expr']
for name, value in self.annotations(request, include_annotations).items()
}

# We do an order by on a copy of annotations so that we see which keys
# it pops
annotations_copy = annotations.copy()
ordering = self._order_by_base(queryset, request, annotations).query.order_by

queryset = queryset.annotate(**{
name: expr
for name, expr in annotations.items()
if name not in annotations_copy
})

try:
obj = self.get_queryset(request).get(pk=int(after_id))
obj = queryset.get(pk=int(after_id))
except (ValueError, self.model.DoesNotExist):
raise BinderRequestError(f'invalid value for after_id: {after_id!r}')

# Now we will build up a comparison expr based on the order by
ordering = self.order_by(self.model.objects.all(), request).query.order_by
left_exprs = []
right_exprs = []

Expand Down Expand Up @@ -1611,7 +1626,7 @@ def _get_filtered_queryset_base(self, request, pk=None, include_annotations=None

annotations = {
name: value['expr']
for name, value in get_annotations(queryset.model, request, include_annotations.get('')).items()
for name, value in self.annotations(request, include_annotations).items()
}

#### filters
Expand All @@ -1635,7 +1650,7 @@ def _get_filtered_queryset_base(self, request, pk=None, include_annotations=None
except KeyError:
pass
else:
expr = self._after_expr(request, after)
expr = self._after_expr(request, after, include_annotations)
queryset = queryset.filter(expr)

return queryset, annotations
Expand Down

0 comments on commit daff032

Please sign in to comment.