原文:http://appengine-cookbook.appspot.com/recipe/how-to-query-by-date-range/
Often, you need to store, and retrieve, data that needs to match an "effective date period", containing a "start date" and "end date". However, if you write a query that tries to compare an input date with both a "start date" field and an "end date" field, like this:
SELECT * FROM MyTable WHERE input_date >= start_date AND input_date <= end_date
the GAE’s datastore will complain: "Inequality Filters Are Allowed On One Property Only". So you need to design your model a little differently.
You can handle your query if you add another field in your Kind, a ListProperty containing both the start date and end date. For example, add a ListProperty named "date_list", and populate it like this: date_list = [start_date, end_date].
Your queries can contain two inequality comparison tests against the same field, and, fortunately for us, queries against a list field do what we need.
A query containing "WHERE input_date >= date_list" matches data in the datastore if the input date is higher than or equal to any date in the list, which is the same result as comparing the input date to the earliest (start) date, and "WHERE input_date <= date_list" matches data if the input date is lower than or equal to any date in the list, which is the same result as comparing to the latest (end) date.
Attached is an example using a real US payroll tax table. As you probably know, the tax tables change every year. A good payroll system will have tax tables stored with "effective dates", so the system can calculate tax amounts for any tax payment period.
The attached model represents, not the tax rate, but the maximum taxable amount for a simple US payroll tax called OASDI (which funds retirement income, part of the Social Security tax). If you are lucky enough to make more than $106,800 this year, you don’t have to pay OASDI tax on the excess, and your net pay goes up even more. Lucky you :-) That $106,800 figure changes every (calendar) year, so a payroll system must store a table of ‘maximum wages subject to OASDI tax, by year’.
示例代码:
http://appengine-cookbook.appspot.com/attachment/?id=ahJhcHBlbmdpbmUtY29va2Jvb2tyEQsSCkF0dGFjaG1lbnQY0ygM
(GAE) How to query by date range
(GAE) How to query by date range
...