DMF*_*rer 5 django django-filter swagger django-rest-framework drf-yasg
在我们的 API 中,我们有一个端点来列出位置。我们允许过滤位置类型,我们允许此过滤器有多个值。例如:
GET /location/?type=hotel&type=airport
Run Code Online (Sandbox Code Playgroud)
对于过滤,我们使用django-filter. 但是,drf-yasg似乎没有正确生成此参数的架构。
视图类可以归结为:
GET /location/?type=hotel&type=airport
Run Code Online (Sandbox Code Playgroud)
过滤器类如下所示:
from rest_framework.generics import ListAPIView
from .models import Location
from .serializers import LocationListSerializer
from .filters import LocationFilterSet
from django_filters.rest_framework import DjangoFilterBackend
class LocationListView(ListAPIView):
queryset = Location.objects.all()
serializer_class = LocationListSerializer
filter_backends = (
DjangoFilterBackend,
)
filter_class = LocationFilterSet
Run Code Online (Sandbox Code Playgroud)
此视图按预期工作 - 以下测试通过:
from django_filters import rest_framework as filters
from .models import Location
class LocationFilterSet(filters.FilterSet):
type = filters.MultipleChoiceFilter(choices=Location.TYPE_CHOICES)
class Meta:
model = Location
fields = (
'type',
)
Run Code Online (Sandbox Code Playgroud)
我希望为这个参数生成的 yaml 看起来像这样:
parameters:
- name: type
in: query
description: ''
required: false
schema:
type: array
items:
type: string
explode: true
Run Code Online (Sandbox Code Playgroud)
但相反,它看起来像这样:
- name: type
in: query
description: ''
required: false
type: string
Run Code Online (Sandbox Code Playgroud)
这是限制drf-yasg吗?
不能使用swagger_auto_schema's,query_serializer因为它不允许覆盖过滤器后端生成的模式。
这似乎是因为django_filters.rest_framework.backends.DjangoFilterBackend.get_coreschema_field只输出两种字段类型,数字和字符串。我继续并覆盖了该方法,但是,它随后在 中引发错误drf_yasg.inspectors.query.CoreAPICompatInspector.coreapi_field_to_parameter,不接受数组类型。
| 归档时间: |
|
| 查看次数: |
1544 次 |
| 最近记录: |