tfranzel / drf-spectacular

Sane and flexible OpenAPI 3 schema generation for Django REST framework.
https://drf-spectacular.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.33k stars 259 forks source link

Add support for displaying ordering fields #766

Open Headmaster11 opened 2 years ago

Headmaster11 commented 2 years ago
class SomeViewSet(mixins.ListModelMixin, GenericViewSet):
    queryset = SomeModel.objects.all()
    filter_backends = (filters.OrderingFilter,)
    ordering_fields = ("field1", "field2")

    @extend_schema(
        parameters=[
            OpenApiParameter(
                name="ordering",
                description=f'Supports sorting by {" ".join(str(x) for x in ordering_fields)}. Use - to filter backwards',
            )
        ]
    )
    def list(self, request, *args, **kwargs):
        return super().list(request, *args, **kwargs)

without extend schema decorator, description for ordering is Which field to use when ordering the results. which is not very informative. Maybe it's better to have list of supported fields?

tfranzel commented 2 years ago

Hi @Headmaster11, that is a good point. The default actually comes out of DRF:

https://github.com/encode/django-rest-framework/blob/71e6c30034a1dd35a39ca74f86c371713e762c79/rest_framework/filters.py#L322

So we may either fix it upstream or introduce an override (OpenApiFilterExtension) on our side.

barseghyanartur commented 1 month ago

What's the status of this?

tfranzel commented 1 month ago

no further work has been done on our side. I think the preferred solution would be to fix it upstream in DRF.