Skip to content

Conversation

@xaptronic
Copy link

Add functionality to allow filtering for datetime type fields by date using {field_name}__date

Description

I have fields in my models/schema that are datetime objects, but I wish to be able to filter for them using only a date (e.g.; filter all entries on a particular date).

This PR adds __date operator that allows for such filtering.

Motivation and Context

There does not appear to be a way to filter datetime fields by a date object. Perhaps it can be done using lte / gte but I would rather specify one filter.. it seems more obvious and less of a work around a missing filter type.

How Has This Been Tested?

Added unit tests and used functionality in my use-case.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 19, 2025

CodSpeed Performance Report

Merging #1854 will not alter performance

Comparing xaptronic:develop (83cd816) with develop (e34c2c0)

Summary

✅ 8 untouched benchmarks

)
)
date = datetime.date(2020, 5, 20)
self.assertEqual(await DatetimeFields.filter(datetime__date=date).count(), 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have more tests for these: checking that this filter modifier actually converts a date time to a date. The provided test case doesn't actually test anything. For instance, in Postgres

SELECT TIMESTAMP '2024-01-20 00:00:00' = DATE '2024-01-20';
> true

@henadzit
Copy link
Contributor

Thanks for looking into this!

Here's a few links to how Django implements this:

In Django they implement the conversion for each database, so this might be more complicated than just using the Date function.

@xaptronic xaptronic closed this Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants