Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
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):?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

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
over 4 years ago · Santiago Trujillo Relatório

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda