Skip to content

Commit

Permalink
fix recipe counting issue on extended mixin
Browse files Browse the repository at this point in the history
  • Loading branch information
smilerz committed Sep 12, 2023
1 parent c72bf57 commit 52909e8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 6 deletions.
3 changes: 1 addition & 2 deletions cookbook/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def get_fields(self, *args, **kwargs):
api_serializer = None
# extended values are computationally expensive and not needed in normal circumstances
try:
if str2bool(
self.context['request'].query_params.get('extended', False)) and self.__class__ == api_serializer:
if str2bool(self.context['request'].query_params.get('extended', False)) and self.__class__ == api_serializer:
return fields
except (AttributeError, KeyError):
pass
Expand Down
6 changes: 2 additions & 4 deletions cookbook/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,7 @@ def annotate_recipe(self, queryset=None, request=None, serializer=None, tree=Fal

# add a recipe count annotation to the query
# explanation on construction https://stackoverflow.com/a/43771738/15762829
recipe_count = Recipe.objects.filter(**{recipe_filter: OuterRef('id')}, space=space).values(
recipe_filter).annotate(count=Count('pk')).values('count')
recipe_count = Recipe.objects.filter(**{recipe_filter: OuterRef('id')}, space=space).values(recipe_filter).annotate(count=Count('pk', distinct=True)).values('count')
queryset = queryset.annotate(recipe_count=Coalesce(Subquery(recipe_count), 0))

# add a recipe image annotation to the query
Expand Down Expand Up @@ -323,8 +322,7 @@ def get_queryset(self):
except self.model.DoesNotExist:
self.queryset = self.model.objects.none()
else:
return self.annotate_recipe(queryset=super().get_queryset(), request=self.request,
serializer=self.serializer_class, tree=True)
return self.annotate_recipe(queryset=super().get_queryset(), request=self.request, serializer=self.serializer_class, tree=True)
self.queryset = self.queryset.filter(space=self.request.space).order_by(Lower('name').asc())

return self.annotate_recipe(queryset=self.queryset, request=self.request, serializer=self.serializer_class,
Expand Down

0 comments on commit 52909e8

Please sign in to comment.