Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
rp
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
14
Issues
14
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
La Quadrature du Net
rpteam
rp
Commits
ba2b3c05
Verified
Commit
ba2b3c05
authored
Sep 06, 2017
by
Thibaut Broggi
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
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
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
11 additions
and
2 deletions
+11
-2
apps/rp/api/urls.py
apps/rp/api/urls.py
+2
-1
apps/rp/api/views.py
apps/rp/api/views.py
+9
-1
No files found.
apps/rp/api/urls.py
View file @
ba2b3c05
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
apps/rp/api/views.py
View file @
ba2b3c05
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
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment