added prefetch select related

This commit is contained in:
Anatoly 2019-10-14 12:24:26 +03:00
parent 9e237d4657
commit 469f7bdf4c
2 changed files with 16 additions and 1 deletions

View File

@ -16,6 +16,7 @@ from phonenumber_field.modelfields import PhoneNumberField
from collection.models import Collection from collection.models import Collection
from location.models import Address from location.models import Address
from main.models import Award from main.models import Award
from tag.models import Tag, TagCategory
from review.models import Review from review.models import Review
from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin, from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
TranslatedFieldsMixin, BaseAttributes) TranslatedFieldsMixin, BaseAttributes)
@ -24,6 +25,18 @@ from utils.models import (ProjectBaseMixin, TJSONField, URLImageMixin,
class EstablishmentTypeQuerySet(models.QuerySet): class EstablishmentTypeQuerySet(models.QuerySet):
"""QuerySet for model EstablishmentType.""" """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 # todo: establishment type&subtypes check
class EstablishmentType(TranslatedFieldsMixin, ProjectBaseMixin): class EstablishmentType(TranslatedFieldsMixin, ProjectBaseMixin):
@ -35,6 +48,8 @@ class EstablishmentType(TranslatedFieldsMixin, ProjectBaseMixin):
help_text='{"en-GB":"some text"}') help_text='{"en-GB":"some text"}')
use_subtypes = models.BooleanField(_('Use subtypes'), default=True) use_subtypes = models.BooleanField(_('Use subtypes'), default=True)
objects = EstablishmentTypeQuerySet.as_manager()
class Meta: class Meta:
"""Meta class.""" """Meta class."""

View File

@ -167,7 +167,7 @@ class EstablishmentTypeRUDView(generics.RetrieveUpdateDestroyAPIView):
class EstablishmentTypeTagListView(generics.ListAPIView): class EstablishmentTypeTagListView(generics.ListAPIView):
"""List of tags with categories by establishment type.""" """List of tags with categories by establishment type."""
serializer_class = serializers.EstablishmentTagsByType serializer_class = serializers.EstablishmentTagsByType
queryset = models.EstablishmentType.objects.all() queryset = models.EstablishmentType.objects.with_base_related()
filter_class = EstablishmentTypeTagFilter filter_class = EstablishmentTypeTagFilter
permission_classes = (permissions.AllowAny, ) permission_classes = (permissions.AllowAny, )
pagination_class = None pagination_class = None