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):?
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
You will need to write your own OrderingFilter based on the Django REST framework one by overriding get_ordering and using a dictionary to map the "short name" to the full queryset string.