django-filters - field_labels不适用于排序过滤器

r7knjye2  于 2022-12-30  发布在  Go
关注(0)|答案(1)|浏览(109)

有一个模型:

class Calendar(models.Model):
    ...
    vendor_code = models.TextField()
    ...

过滤器集类:

from django_filters import OrderingFilter
from django_filters.rest_framework import FilterSet, DjangoFilterBackend

class CalendarFilter(FilterSet):

    ordering = OrderingFilter(
        fields=(
            ('vendor_code', 'vendor_code'),
        ),
        field_labels={
            'vendor_code': 'Vendor code',
        }
    )

    class Meta:
        model = Calendar
        fields = ['vendor_code']

该视图:

class CalendarsView(mixins.ListModelMixin, GenericViewSet):
    ...
    filter_backends = (DjangoFilterBackend,)
    filterset_class = CalendarFilter

我希望如果定义field_labels参数,标签Vendor code会显示为swagger,但只有ordering,没有任何描述:

我需要这个为了前端开发人员可以看到他们。
我哪里做错了?

zzwlnbp8

zzwlnbp81#

OrderingFilter在视图中的使用方式如下:

class CalendarsView(mixins.ListModelMixin, GenericViewSet):
    ...
    filter_backends = [DjangoFilterBackend, filters.SearchFilter,filters.OrderingFilter]
    filter_class = CalendarFilter
    filter_backends = [filters.OrderingFilter]

并且在CalendarFilter使用字段中:

class CalendarFilter(FilterSet):
    vendor_code = filters.CharFilter(field_name='vendor_code', lookup_expr='icontains', label='vendor_code')

相关问题