views:

45

answers:

2

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 than 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.

A: 

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)
S.Lott
It looks like it should work yet on the development server it raises my TypeError: filter() got an unexpected keyword argument 'modified__gte' . How could I proceed?
LarsOn
@S.Lott He's not using Django. Which is why your example doesn't work.
Nick Johnson
It works using lambda or the conditional for statement above. Thank you
LarsOn
+1  A: 

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.

Nick Johnson