I didn't find a way, how to show possible values for OrderingFilter and show searchable fields for SearchFilter

from rest_framework.filters import SearchFilter, OrderingFilter
class MyViewSet:
filter_backends = [DjangoFilterBackend, SearchFilter, OrderingFilter]
search_fields = ['name',
'slug',
'office_phone',
'street_line1',
'street_line2',
'zipcode',
'state',
'city__name',
'city__county__name']
ordering_fields = ['name', 'state']
Or I should manually enter them in description ?
I'm struggling with the same problem. Have you found an answer yet?
@rlrglobecomm no:(
I figured it out. I was able to override the "get_schema_fields" method in filters.SearchFilter, and alter the result by merging my description into the base class result.
I used the base class 'get_search_fields' to get a list of the search fields from within my override, then formatted an appropriate description string. From that, I merged this description into the 'description' of a new coreapi.Field instance and returned that new coreapi.Field instance to the caller.
There is a sample of overriding the search class here:
https://www.django-rest-framework.org/api-guide/filtering/#searchfilter
In my case, I use viewsets.ModelViewSet as my view base, and had to include my custom searchFilter into the filter_backends list of that viewSet.
Also, I did have to add imports coreapi and coreschema from rest_framework.compat.
I hope this helps.
`
class DocEnhancementSearchFilter(filters.SearchFilter):
def get_schema_fields(self,view):
sf_result = str(super().get_search_fields(view,None))
sf_result = sf_result.replace("'","").replace("(","").replace(")","").replace("__","/")
result = super().get_schema_fields(view)
newField = coreapi.Field(
name=result[0].name ,
required=result[0].required, location=result[0].location,
schema=coreschema.String(
title="Search",
description="Word Search within these fields: " + sf_result
)
)
return [newField]
`
@rlrglobecomm try to push it to upstream.
Not much to change, but a nice QOL improvement, I've had to do exactly the same as you, I'm sure there are a lot of us.
And ordering filter too, I feel it is even more relevant.
possible values for OrderingFilter
class YourOrderingFilter(filters.OrderingFilter):
def get_schema_fields(self, view):
self.ordering_description = "Fields for sorting: " + ', '.join(view.ordering_fields)
return super().get_schema_fields(view)
view.ordering_fields gets from ordering_fields in ListAPIView
class YourListView(ListAPIView):
...
filter_backends = [ScoresOrderingFilter]
ordering_fields = ['title', 'balance']
Most helpful comment
`
class DocEnhancementSearchFilter(filters.SearchFilter):
`