Verified Commit ba2b3c05 authored by Thibaut Broggi's avatar Thibaut Broggi
Browse files

Add a search route in the API

    It can search for one word in the title of the article
parent c0d4e1a3
Pipeline #1199 passed with stages
in 2 minutes and 1 second
from rest_framework import routers
from .views import ArticleViewSet
from .views import ArticleViewSet, ArticleSearch
router = routers.DefaultRouter()
router.register(r"articles", ArticleViewSet)
router.register(r"articles-search/(?P<search_keywords>.+)", ArticleSearch)
urlpatterns = router.urls
from rest_framework import viewsets
from rest_framework import viewsets, mixins
from rp.models import Article
from .serializers import ArticleSerializer
......@@ -10,3 +10,11 @@ ArticleMixin = get_viewset_transition_actions_mixin(Article)
class ArticleViewSet(ArticleMixin, viewsets.ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
def list(self, request, *args, **kwargs):
self.queryset = self.queryset.filter(title__icontains=kwargs['search_keywords'])
return super().list(request, args, kwargs)
Supports Markdown
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