How to compare python date with DateTimeProperty in NDB request

I'm having difficulty trying to use a python date object to filter using DateTimeProperty in an NDB request. I understand that DateTimeProperty (some_date) does not have a date property, but is there anything simple I can do that looks like this:

cls.query(supplied_date == cls.some_date.date).fetch() 
+4
source share
1 answer

This is not possible without changing your model.

The simplest option would be to use two inequality filters in the datetime property (> date 00:00 and <= date 23:59), but then you could not use any other inequality in your queries.

Another option (an?) Is to introduce a second property that contains the date value you are filtering:

 class YourModel(ndb.Model): some_datetime = ndb.DateTimeProperty() some_date = ndb.ComputedProperty(lambda self: self.some_datetime.date) 

If you only need to filter by date, then your datetime property may have indexed=False , but otherwise you will have the cost of the additional index.

+2
source

Source: https://habr.com/ru/post/1468994/


All Articles