Rename RelatedField ordering filter in django rest framework

django, django-rest-framework

Solution

It can be achieved with a minor change in `def get_queryset(self)`.

def get_queryset(self):
    query = super(UserListView, self).get_queryset().annotate(profession=F('profile__profession'))
    return query

Problem

In a django rest framework APIView we specify ordering fields using the same method as the search filters and therefore we can specify ordering using related names. ``` ordering_fields = ('username', 'email', 'profile__profession') ``` The route would look like this: `https://example.com/route?ordering=profile__profession` However we would rather avoid to display the relation between the models in the api and then specify `profession` instead of `profile__profession`. Such as `https://example.com/route?ordering=profession` Can this be achieved without having to implement the sorting in the `APIView`'s `def get_queryset(self):`?

Original source