From 469f7bdf4cb506c8a317e3a127a96bff73cdc0e1 Mon Sep 17 00:00:00 2001 From: Anatoly Date: Mon, 14 Oct 2019 12:24:26 +0300 Subject: [PATCH] added prefetch select related --- apps/establishment/models.py | 15 +++++++++++++++ apps/establishment/views/back.py | 2 +- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/apps/establishment/models.py b/apps/establishment/models.py index 77992f9d..195f029d 100644 --- a/apps/establishment/models.py +++ b/apps/establishment/models.py @@ -16,6 +16,7 @@ from phonenumber_field.modelfields import PhoneNumberField from collection.models import Collection from location.models import Address from main.models import Award +from tag.models import Tag, TagCategory from review.models import Review from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin, TranslatedFieldsMixin, BaseAttributes) @@ -24,6 +25,18 @@ from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin, class EstablishmentTypeQuerySet(models.QuerySet): """QuerySet for model EstablishmentType.""" + def with_base_related(self): + """Return QuerySet with base related.""" + return self.prefetch_related( + models.Prefetch('tag_categories', + EstablishmentTypeTagCategory.objects.select_related('tag_category')), + models.Prefetch('establishmentsubtype_set', + EstablishmentSubType.objects.prefetch_related( + models.Prefetch( + 'tag_categories', + EstablishmentSubTypeTagCategory.objects.select_related('tag_category')))) + ) + # todo: establishment type&subtypes check class EstablishmentType(TranslatedFieldsMixin, ProjectBaseMixin): @@ -35,6 +48,8 @@ class EstablishmentType(TranslatedFieldsMixin, ProjectBaseMixin): help_text='{"en-GB":"some text"}') use_subtypes = models.BooleanField(_('Use subtypes'), default=True) + objects = EstablishmentTypeQuerySet.as_manager() + class Meta: """Meta class.""" diff --git a/apps/establishment/views/back.py b/apps/establishment/views/back.py index a5e2ff47..543dbd3e 100644 --- a/apps/establishment/views/back.py +++ b/apps/establishment/views/back.py @@ -167,7 +167,7 @@ class EstablishmentTypeRUDView(generics.RetrieveUpdateDestroyAPIView): class EstablishmentTypeTagListView(generics.ListAPIView): """List of tags with categories by establishment type.""" serializer_class = serializers.EstablishmentTagsByType - queryset = models.EstablishmentType.objects.all() + queryset = models.EstablishmentType.objects.with_base_related() filter_class = EstablishmentTypeTagFilter permission_classes = (permissions.AllowAny, ) pagination_class = None