ingredient related recipes performance

This commit is contained in:
vabene1111 2023-02-26 08:27:20 +01:00
parent 38010117e5
commit 1efcf386e2
2 changed files with 9 additions and 2 deletions

View File

@ -627,7 +627,11 @@ class IngredientSimpleSerializer(WritableNestedModelSerializer):
nutritions = serializers.SerializerMethodField('get_nutritions')
def get_used_in_recipes(self, obj):
return list(Recipe.objects.filter(steps__ingredients=obj.id).values('id', 'name'))
used_in = []
for s in obj.step_set.all():
for r in s.recipe_set.all():
used_in.append({'id':r.id,'name':r.name})
return used_in
def get_conversions(self, obj):
conversions = []

View File

@ -797,7 +797,10 @@ class RecipeViewSet(viewsets.ModelViewSet):
if self.detail: # if detail request and not list, private condition is verified by permission class
if not share: # filter for space only if not shared
self.queryset = self.queryset.filter(space=self.request.space).prefetch_related('steps', 'keywords',
'shared', 'steps__ingredients',
'shared',
'steps__ingredients',
'steps__ingredients__step_set',
'steps__ingredients__step_set__recipe_set',
'steps__ingredients__food',
'steps__ingredients__food__inherit_fields',
'steps__ingredients__food__supermarket_category',