开发者

Slicing a result list by time value

I got a result list and want to keep the elements that are newer than timeline and older than bookmark. Is there a more convenient method than iterating the whole list and removing the elements if they match the conditition? Can you introduce me to how specically how? The way I fetch data and then sort it

results = A.all().search(self.request.get('q')).filter("published =", True)
results = sorted(results, key=lambda x: x.modified, reverse=True)

Then I want to keep elements older than bookmark and newer tha开发者_C百科n timeline where these variables are defined by HTTP GET or if blank defined as

bookmark = datetime.now()
timeline = datetime.now () - timedelta (days = 50)

I hope you understand what I'm trying to do (it's like paging) and thank you in advance for any ideas.


If you're using the SearchableModel and doing a search query (which it would appear you are, from the snippet), you can't apply sort orders or inequality filters without requiring exploding indexes, as established in your previous question on the topic. Thus, you can't apply these filters as part of the query - so filtering the results manually is your best (and only) option.


This?

bookmark = datetime.now()
timeline = datetime.now () - timedelta (days = 50)


results = A.all().search(self.request.get('q')).filter("published =", True)
results = results.filter( modified__gte=bookmark ).filter( modified__lte=timeline )
results = sorted(results, key=lambda x: x.modified, reverse=True)

Or this?

bookmark = datetime.now()
timeline = datetime.now () - timedelta (days = 50)

results = A.all().search(self.request.get('q')).filter("published =", True)
results = [ r for r in results if r.modified >= bookmark and r.modified <= timeLine ]
results = sorted(results, key=lambda x: x.modified, reverse=True)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜