如何在 Django REST 框架中更改 URL 中的默认搜索查询参数?
How to change the default search query parameter in URL in Django REST framework?
在 Django REST 框架中。默认情况下,它在搜索任何内容时使用 - /?search= in URL 。
例如 http://127.0.0.1:8000/api/branches/?search=RTGS
而这个url成功得到结果。
但是我需要将 URL 更改为 http://127.0.0.1:8000/api/branches/autocomplete?q=RTGS
在文档中,https://www.django-rest-framework.org/api-guide/settings/#search_param
假设它是默认设置的。 https://www.django-rest-framework.org/api-guide/settings/#search_paramd 我们可以改变。
我想知道如何。
谢谢
https://www.django-rest-framework.org/api-guide/settings/#search_param
urls.py
来自 django.urls 导入路径,包括
从 。导入视图
来自 rest_framework 导入路由器
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
来自the docs:
By default, the search parameter is named 'search'
, but this may be overridden with the SEARCH_PARAM
setting.
因此,在您的 settings.py
:
REST_FRAMEWORK = {
'SEARCH_PARAM': 'q'
}
编辑:
在这里你可以看到实际的代码:
设置:https://github.com/encode/django-rest-framework/blob/master/rest_framework/settings.py#L68
过滤器:https://github.com/encode/django-rest-framework/blob/master/rest_framework/filters.py#L42
如果您只想在一个视图中更改查询参数键 ,您可以扩展 SearchFilter,然后将其添加到您的 filter_backends
查看。
class CustomSearchFilter(SearchFilter):
search_param = "q"
class MyListView(ListAPIView):
# ...
filter_backends = [CustomSearchFilter]
# ...
在 Django REST 框架中。默认情况下,它在搜索任何内容时使用 - /?search= in URL 。 例如 http://127.0.0.1:8000/api/branches/?search=RTGS 而这个url成功得到结果。 但是我需要将 URL 更改为 http://127.0.0.1:8000/api/branches/autocomplete?q=RTGS
在文档中,https://www.django-rest-framework.org/api-guide/settings/#search_param 假设它是默认设置的。 https://www.django-rest-framework.org/api-guide/settings/#search_paramd 我们可以改变。 我想知道如何。
谢谢
https://www.django-rest-framework.org/api-guide/settings/#search_param
urls.py 来自 django.urls 导入路径,包括 从 。导入视图 来自 rest_framework 导入路由器
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
来自the docs:
By default, the search parameter is named
'search'
, but this may be overridden with theSEARCH_PARAM
setting.
因此,在您的 settings.py
:
REST_FRAMEWORK = {
'SEARCH_PARAM': 'q'
}
编辑:
在这里你可以看到实际的代码:
设置:https://github.com/encode/django-rest-framework/blob/master/rest_framework/settings.py#L68
过滤器:https://github.com/encode/django-rest-framework/blob/master/rest_framework/filters.py#L42
如果您只想在一个视图中更改查询参数键 ,您可以扩展 SearchFilter,然后将其添加到您的 filter_backends
查看。
class CustomSearchFilter(SearchFilter):
search_param = "q"
class MyListView(ListAPIView):
# ...
filter_backends = [CustomSearchFilter]
# ...