base related qs for cities

This commit is contained in:
Kuroshini 2020-01-27 18:45:23 +03:00
parent b6bfc51aad
commit f5da5f71a3
4 changed files with 7 additions and 4 deletions

View File

@ -353,7 +353,7 @@ class EstablishmentNoteListCreateView(EstablishmentMixinViews,
lookup_field = 'slug'
serializer_class = serializers.EstablishmentNoteListCreateSerializer
permission_classes = (permissions.AllowAny, )
def get_object(self):
"""Returns the object the view is displaying."""
establishment_qs = models.Establishment.objects.all()

View File

@ -139,6 +139,9 @@ class CityQuerySet(models.QuerySet):
"""Return establishments by country code"""
return self.filter(country__code=code)
def with_base_related(self):
return self.prefetch_related('country', 'region', 'region__country')
class City(models.Model):
"""Region model."""

View File

@ -44,7 +44,7 @@ class CityListCreateView(common.CityViewMixin, generics.ListCreateAPIView):
def get_queryset(self):
"""Overridden method 'get_queryset'."""
qs = models.City.objects.all().annotate(locale_name=KeyTextTransform(get_current_locale(), 'name_translated'))\
.order_by('locale_name')
.order_by('locale_name').with_base_related()
if self.request.country_code:
qs = qs.by_country_code(self.request.country_code)
return qs

View File

@ -24,7 +24,7 @@ class RegionViewMixin(generics.GenericAPIView):
class CityViewMixin(generics.GenericAPIView):
"""View Mixin for model City"""
model = models.City
queryset = models.City.objects.all()
queryset = models.City.objects.with_base_related()
class AddressViewMixin(generics.GenericAPIView):
@ -101,7 +101,7 @@ class CityListView(CityViewMixin, generics.ListAPIView):
def get_queryset(self):
qs = super().get_queryset()
if self.request.country_code:
qs = qs.by_country_code(self.request.country_code)
qs = qs.by_country_code(self.request.country_code).with_base_related()
return qs