moved annotation to default query manager

This commit is contained in:
vabene1111
2022-09-16 18:18:59 +02:00
parent 947986277a
commit e391abd23d
3 changed files with 6 additions and 5 deletions

View File

@@ -123,8 +123,6 @@ class RecipeSearch():
def get_queryset(self, queryset): def get_queryset(self, queryset):
self._queryset = queryset self._queryset = queryset
self._queryset = self._queryset.prefetch_related('keywords') self._queryset = self._queryset.prefetch_related('keywords')
self._queryset = self._queryset.annotate(rating=Avg('cooklog__rating'))
self._queryset = self._queryset.annotate(last_cooked=Max('cooklog__created_at'))
self._build_sort_order() self._build_sort_order()
self._recently_viewed(num_recent=self._num_recent) self._recently_viewed(num_recent=self._num_recent)

View File

@@ -14,7 +14,7 @@ from django.contrib.postgres.search import SearchVectorField
from django.core.files.uploadedfile import InMemoryUploadedFile, UploadedFile from django.core.files.uploadedfile import InMemoryUploadedFile, UploadedFile
from django.core.validators import MinLengthValidator from django.core.validators import MinLengthValidator
from django.db import IntegrityError, models from django.db import IntegrityError, models
from django.db.models import Index, ProtectedError, Q from django.db.models import Index, ProtectedError, Q, Avg, Max
from django.db.models.fields.related import ManyToManyField from django.db.models.fields.related import ManyToManyField
from django.db.models.functions import Substr from django.db.models.functions import Substr
from django.utils import timezone from django.utils import timezone
@@ -722,6 +722,10 @@ class NutritionInformation(models.Model, PermissionModelMixin):
# space = models.ForeignKey(Space, on_delete=models.CASCADE) # space = models.ForeignKey(Space, on_delete=models.CASCADE)
# objects = ScopedManager(space='space') # objects = ScopedManager(space='space')
class RecipeManager(models.Manager.from_queryset(models.QuerySet)):
def get_queryset(self):
return super(RecipeManager, self).get_queryset().annotate(rating=Avg('cooklog__rating')).annotate(last_cooked=Max('cooklog__created_at'))
class Recipe(ExportModelOperationsMixin('recipe'), models.Model, PermissionModelMixin): class Recipe(ExportModelOperationsMixin('recipe'), models.Model, PermissionModelMixin):
name = models.CharField(max_length=128) name = models.CharField(max_length=128)
@@ -753,7 +757,7 @@ class Recipe(ExportModelOperationsMixin('recipe'), models.Model, PermissionModel
desc_search_vector = SearchVectorField(null=True) desc_search_vector = SearchVectorField(null=True)
space = models.ForeignKey(Space, on_delete=models.CASCADE) space = models.ForeignKey(Space, on_delete=models.CASCADE)
objects = ScopedManager(space='space') objects = ScopedManager(space='space', _manager_class=RecipeManager)
def __str__(self): def __str__(self):
return self.name return self.name

View File

@@ -785,7 +785,6 @@ class RecipeViewSet(viewsets.ModelViewSet):
share = self.request.query_params.get('share', None) share = self.request.query_params.get('share', None)
if self.detail: # if detail request and not list, private condition is verified by permission class if self.detail: # if detail request and not list, private condition is verified by permission class
self.queryset = self.queryset.prefetch_related('keywords').annotate(rating=Avg('cooklog__rating')).annotate(last_cooked=Max('cooklog__created_at'))
if not share: # filter for space only if not shared if not share: # filter for space only if not shared
self.queryset = self.queryset.filter(space=self.request.space) self.queryset = self.queryset.filter(space=self.request.space)
return super().get_queryset() return super().get_queryset()