elynch
elynch

Reputation: 2240

Order by DateTimeRange field on a django model

In my django project, I have a model with a Postgres DateTimeRangeField holding the time lapse between the start and the end of a long task. Something like this:

timespan = DateTimeRangeField()

Is it possible to use order_by on the queryset using either the lower or upper attributes of the range field?

Upvotes: 2

Views: 1815

Answers (1)

dani herrera
dani herrera

Reputation: 51665

Using either the lower or upper attributes of the range field is the order by default behaviour. Quoting DateTimeRangeField django docs:

Range fields support the standard lookups: lt, gt, lte and gte. These are not particularly helpful - they compare the lower bounds first and then the upper bounds only if necessary. This is also the strategy used to order by a range field. It is better to use the specific range comparison operators

Upvotes: 1

Related Questions