Skip to content

OrderingFilter does not accept related fields (relation.field / relation__field) in sort? #1317

Closed

Description

@jaimeres

Hi,

I'm trying to sort a resource by a field from a related model using the ?sort=
Current behavior: When I send ?sort=responsible_level.level, the API returns:

{ "errors": [{ "detail": "invalid sort parameter: responsible_level.level", "status": "400", "code": "invalid" }] }

Expected behavior:
Be able to sort by a field on a related model, e.g. ?sort=responsible_level.level mapping to the ORM lookup responsible_level__level.

Questions:

Does OrderingFilter support sorting by relationship fields using dot notation (relationship.field or relationship__field)?
If not, what is the recommended approach to allow sorting by a related model's field while staying compliant with the JSON:API spec?
Is there a way to define aliases in ordering_fields that map to ORM __ lookups for related fields?

Thanks!

Activity

  1. sliverc commented on Mar 27, 2026

    @sliverc
    Member

    Thanks for your message. In the view you want to sort how did you define ordering_filelds?

    This is an internal field so you should define it with a __ but in the API call (query param) you can then use a dot notation.

    class TestView(ViewSet):
        ordering_fields = ("responsible_level__level", "id")

    define all fields you want to allow to be ordered too. Does this work?

  2. sliverc commented on Oct 8, 2026

    @sliverc
    Member

    As noted in comment ordering_fields needs to be defined so sorting of a relation field is possible.

    #1334 added some clarification in the docs about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions