Add route /articls-by-tag in API

parent 566e90f9
Pipeline #1206 passed with stages
in 1 minute and 59 seconds
from rest_framework import routers
from .views import ArticleViewSet, ArticleSearch
from .views import ArticleViewSet, ArticleSearch, ArticleTag
router = routers.DefaultRouter()
router.register(r"articles", ArticleViewSet)
router.register(r"articles-by-tag/(?P<filter_tag>.+)", ArticleTag)
router.register(r"articles-search/(?P<search_keywords>.+)", ArticleSearch)
urlpatterns = router.urls
......@@ -18,3 +18,12 @@ class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
def list(self, request, *args, **kwargs):
self.queryset = self.queryset.filter(title__icontains=kwargs['search_keywords'])
return super().list(request, args, kwargs)
class ArticleTag(viewsets.ModelViewSet, mixins.ListModelMixin):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
def list(self, request, *args, **kwargs):
print(kwargs['filter_tag'])
self.queryset = self.queryset.filter(tags__name__in=[kwargs['filter_tag']])
return super().list(request, args, kwargs)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment