filtration by country code

This commit is contained in:
Kirill 2020-02-03 19:18:23 +03:00
parent 33942b70b5
commit fd6aced51e
2 changed files with 9 additions and 1 deletions

View File

@ -8,6 +8,7 @@ class CityBackFilter(filters.FilterSet):
"""Employee filter set."""
search = filters.CharFilter(method='search_by_name')
country_code = filters.CharFilter(method='filter_by_country_code')
class Meta:
"""Meta class."""
@ -15,8 +16,15 @@ class CityBackFilter(filters.FilterSet):
model = models.City
fields = (
'search',
'country_code',
)
def filter_by_country_code(self, queryset, name, value):
"""Filter by country code."""
if value not in EMPTY_VALUES:
return queryset.by_country_code(value)
return queryset
def search_by_name(self, queryset, name, value):
"""Search by name or last name."""
if value not in EMPTY_VALUES:

View File

@ -52,7 +52,7 @@ class CityListCreateView(common.CityViewMixin, generics.ListCreateAPIView):
"""Overridden method 'get_queryset'."""
qs = models.City.objects.all().annotate(locale_name=KeyTextTransform(get_current_locale(), 'name'))\
.order_by('locale_name').with_base_related()
if self.request.country_code:
if self.request.country_code and self.request.query_params.get('country_code') is None:
qs = qs.by_country_code(self.request.country_code)
return qs