I am using the following class to store some data:
class NewsArticle(db.Model):
score = db.FloatProperty(default=0.0)
date_scored = db.DateTimeProperty()
...
What I need to do is to get those NewsArticle entities that have the top score in some time frame (e.g. get the top scored data entities of today or of last week).
I tried the following:
query = db.GqlQuery('SELECT * FROM NewsArticle WHERE date_created > DATETIME(:year, :month, :day, 0, 0, 0) ORDER BY score DESC', year=date.selected_year, month=date.selected_month, day=date.selected_day)
But that doesn’t work since the datastore requires that the
first ordering property must be the
same as inequality filter property
I thought about getting all NewsArticle entities for a specific timeframe and then doing the score-sorting in my application, but I am expecting a really large number of results so in-memory-sorting won’t be efficient.
What other solutions are possible for my problem?
You could either:
filter only by the timeframe and sort by score in memory or,
if you can limit the timeframes to whole days and weeks, include additional properties in your model to save the the week as a integer and the day as a
DatePropertyand do a simple equality check on that.EDIT: To learn more, have a look at Restrictions on Queries