Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

312
Views
Rename RelatedField ordering filter in django rest framework

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):?

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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
about 4 years ago · Santiago Trujillo Report

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!