如何正确记录查询参数(搜索,过滤)Django Rest Framework?

时间:2018-11-13 12:34:44

标签: python django django-rest-framework

我是DRF渲染文档的新手。 我不明白如何正确地在代码中对其进行文档化以呈现文档中的描述。我正在使用DRF Docs

例如:我有一条路线,可以在其中检索一些数据。在相关视图中,我有:

search_fields = ('name', 'registration_date')

但是这些字段在文档页面上没有描述。 enter image description here

所以我想为他们添加描述。有办法吗?

2 个答案:

答案 0 :(得分:0)

基本上,您正在寻找REST“模式”功能。 看这里 : REST-SCHEMA

使用此工具,您可以缓慢但不失所愿地向所有希望添加的字段添加说明。

这是一个很好的方法页面: Tutorial 7: Schemas & client libraries

答案 1 :(得分:0)

摘要

DRF文档中有三种类型的描述(正如我尝试过的),它们依赖于两个位置的设置。

  • 类型1 :路径参数说明->在模型字段定义中设置
  • 类型2 :查询参数说明->在过滤器定义中为特定字段设置
  • 类型3 :请求正文说明->与类型1相同

类型1和类型3描述的模型字段定义

# my_project/my_app/models.py
from django.db import models
from django.utils.translation import gettext_lazy as _

class MyModel(models.Model):
    id = models.BigAutoField(primary_key=True, help_text=_("Field id - This will show up in DRF docs Path Parameter Description/Request Body Description, '_' meaning using django translation module"))
    field1 = models.IntegerField(help_text=_("Field field1 - This will show up in DRF docs Path Parameter Description/Request Body Description, '_' meaning using django translation module"))

用于类型2描述的特定字段定义的SearchFilter

# my_project/my_app/filters.py
from rest_framework import filters

class MyFilter(filters.SearchFilter):
    search_param = 'field1'
    search_title = 'Exact matches Field1' # Shows up in DRF docs interactive query pop-up menu as the title for the query section of the field1
    search_description = 'This will show up in DRF docs Query Parameter Description'

序列化器和ViewSet配置

# my_project/my_app/serializers.py
from rest_framework import serializers

from my_app.models import MyModel

class MySerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel
        fields = '__all__'

# my_project/my_app/views.py
from rest_framework import viewsets

from my_app.models import MyModel
from my_app.serializers import MySerializer
from my_app.filters import MyFilter

class MyViewSets(viewsets.ModelViewSet):
    queryset = MyModel.objects.all()
    serializer_class = MySerializer
    filter_backends = [MyFilter]
    search_fields = ['=field1', '@field1']
        # The first charactor '='/'^' is used by SearchFilter.filter_queryset while doing
        # the SearchFilter.construct_search method
        # and '=' for exact match while '^' for starts with search
        # One could get all options at rest_framework.filters.SearchFilter.lookup_prefixes
        # BTW my rest_framework version is djangorestframework==3.11.1

最后是网址和设置配置

# my_project/my_app/urls.py
from django.urls import path, include
from rest_framework import routers

from my_app.views import MyViewSets

router = routers.DefaultRouter()
router.register('test', MyViewSets)

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

# my_project/my_project/urls.py
from django.urls import path, include
from rest_framework.documentation import include_docs_urls

urlpatterns = [
    path('docs/', include_docs_urls('API Documentaion')),
    path('my_app/', include('my_app.urls'))
]

此处演示:drf_docs_description