The default behavior of REST framework’s generic list views is to return the
entire queryset for a model manager. We should overwrite the default
get_queryset method;
# articles/views.pyfromrest_frameworkimportgenericsfromsrc.articles.modelsimportArticlefromsrc.articles.serializerimportArticleSerializerclassArticleListView(generics.ListCreateAPIView):queryset=Article.objects.all()serializer_class=ArticleSerializerregion_separator=","defget_queryset(self):"""
Optionally restricts the returned articles to given regions,
by filtering against a `regions` query parameter in the URL.
"""regions=self.request.query_params.get("regions",None)ifregions:qs=Article.objects.filter()forregioninregions.split(self.region_separator):qs=qs.filter(regions__code=region)returnqsreturnsuper().get_queryset()
It filters articles when regions query param provided, when there is no param
it returns all articles.