2

In Django REST Framework. By default it uses - /?search= in URL while searching for anything. For Example http://127.0.0.1:8000/api/branches/?search=RTGS And this url successfully getting the result. But I need to change the URL to http://127.0.0.1:8000/api/branches/autocomplete?q=RTGS

In the documentation, https://www.django-rest-framework.org/api-guide/settings/#search_param It is given that it is set by default. https://www.django-rest-framework.org/api-guide/settings/#search_paramd we can change. I am wondering how.

Thanks

https://www.django-rest-framework.org/api-guide/settings/#search_param

urls.py from django.urls import path, include from . import views from rest_framework import routers

router = routers.DefaultRouter()
# router.register('bank', views.BankView)
router.register('branches/autocomplete', views.BankDetailView)
# router.register('branches/list', views.BankAPIListView)



urlpatterns = [
    path('api/', include(router.urls)),

]

views.py

from django.shortcuts import render, redirect
from rest_framework import viewsets
from .models import Branches
from .serializers import BranchesSerializer
from rest_framework import filters
from rest_framework.filters import OrderingFilter
from rest_framework.pagination import PageNumberPagination  
# from django_filters.rest_framework import DjangoFilterBackend





class BankDetailView(viewsets.ModelViewSet):
    queryset = Branches.objects.all()
    serializer_class = BranchesSerializer
    filter_backends = [filters.SearchFilter, OrderingFilter]
    # Partial Search with the field in branch
    search_fields = ['^branch']
    # Ordering Filter field by ifsc in ascending order
    # filter_backends = [DjangoFilterBackend]
    # filterset_fields = ['ifsc']

serializers.py

from rest_framework import serializers
from .models import Branches

class BranchesSerializer(serializers.HyperlinkedModelSerializer):
    class Meta :
        model = Branches
        fields = ['url' ,'ifsc', 'bank_id', 'branch', 'address', 'city', 
'district', 'state']

http://127.0.0.1:8000/api/branches/autocomplete?q=RTGS&limit=3&offset=0

Manish Gupta
  • 31
  • 1
  • 5

2 Answers2

3

From the docs:

By default, the search parameter is named 'search', but this may be overridden with the SEARCH_PARAM setting.

Thus, in your settings.py:

REST_FRAMEWORK = {
    'SEARCH_PARAM': 'q'
}

EDIT:

Here you can see the actual code:

Settings: https://github.com/encode/django-rest-framework/blob/master/rest_framework/settings.py#L68

Filters: https://github.com/encode/django-rest-framework/blob/master/rest_framework/filters.py#L42

wencakisa
  • 5,850
  • 2
  • 15
  • 36
  • I added this. And did search again. The result were same as http://127.0.0.1:8000/api/branches/?search=RTGS. I tried changing the same, but won't happening anything. Is there anything somthing extra to add in settings.py before setting this. – Manish Gupta Aug 29 '19 at 07:56
  • It's working acutally. What if I want to make link look like this ; http://127.0.0.1:8000/api/branches/autocomplete?q=RTGS – Manish Gupta Aug 29 '19 at 08:01
  • @ManishGupta Well, I see you've already registered your `BankDetailView` in a router, but routers are used for viewsets only. Try registering it in `urls.py` as a simple `path('branches/autocomplete', BankDetailView.as_view(), name='bank-detail')` inside `urlpatterns = []`. – wencakisa Aug 29 '19 at 08:03
1

If you'd like to change query parameter key in only one view, you can extend SearchFilter and then add it to filter_backends of your view.

class CustomSearchFilter(SearchFilter):
    search_param = "q"


class MyListView(ListAPIView):
    # ...
    filter_backends = [CustomSearchFilter]
    # ...
Eray Erdin
  • 2,633
  • 1
  • 32
  • 66