graphql-python / graphql-python/graphene-django

Cannot order by two fields using django filter

Open
#914 1 comment 2 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
4.4k
Forks
760
PR merge metrics
No merged PRs in 30d

Description

Hi,

I am using `DjangoFilterConnectionField` in my project like this:

`all_sessions = DjangoFilterConnectionField(SessionNode, filterset_class=AgendaFilter)`

`SessionNode` is created based on `Session` model in my Django application. Now, I would like to be able to order these sessions by two fields: `start_date` and `start_time`.

To achieve that I've created the following filter:

```python
class AgendaFilter(FilterSet):
class Meta:
model = Session
exclude = []

order_by = OrderingFilter(
fields=(
("start_date", "start_date"),
("start_time", "start_time")
)
)
```

When I filter sessions by only one field using `orderBy` , the query results are ordered correctly as expected. When I try to use both fields in the filter (shown below), the results returned are not ordered according to either of them:

```graphql
{
allSessions(orderBy: "[start_date, start_time]") {
edges {
node {
id
startDate
startTime
}
}
}
}
```

I've tried different ways of passing the two fields to `orderBy`, but none of them worked for me. How can I correctly order by `start_date` and then by `start_time` in one query? According to the [graphene documentation](https://docs.graphene-python.org/projects/django/en/latest/filtering/), this is possible:

> Ordering
> You can use OrderFilter to define how you want your returned results to be ordered.
>
> Extend the tuple of fields if you want to order by more than one field.

Is this is a bug in graphene or am I doing something wrong?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.