2

私はこのモデルを持っています:

class Vehicle(db.Model):
    ...
    start_production_date = db.DateProperty()
    end_production_date = db.DateProperty()

たとえば、2010 年に生産されたすべての車両をフィルタリングする必要があります。

私はできると思った:

q = (Vehicle.all()
    .filter('start_production_date >=', datetime(2010, 1, 1))
    .filter('end_production_date <', datetime(2011, 1, 1)))

買うBadFilterError:

BadFilterError: invalid filter: Only one property per query may have inequality filters (<=, >=, <, >)..

それで、どうすればこれを達成できますか?さらに、これは非常に一般的なタスクのように思えます。

4

2 に答える 2

2

1 つのアプローチは、モデルを次のように変更することです。

class Vehicle(db.Model):
    ...
    start_production_date = db.DateProperty()
    start_production_year = db.IntegerProperty()
    start_production_month = db.IntegerProperty()
    start_production_day = db.IntegerProperty()

    end_production_date = db.DateProperty()
    end_production_year = db.IntegerProperty()
    end_production_month = db.IntegerProperty()
    end_production_day = db.IntegerProperty()

すべての put でこれらの新しい値を更新し ( put をオーバーライドできます)、単純に:

# specific year

q = (Vehicle.all()
    .filter('start_production_year =', 2010))

# specific year, different months

q = (Vehicle.all()
    .filter('start_production_year =', 2010)
    .filter('start_production_month IN', [1, 2, 3, 4]))

# different years

q = (Vehicle.all()
    .filter('start_production_year IN', [2010, 2011, 2012]))
于 2013-02-18T15:34:57.023 に答える
2

私はこの解決策に行きました:

ListPropertyモデルが作成されたすべての年を含むアイテムをモデルに設定しました。

vhl.production_years = range(start_production_date.year, end_production_date + 1)

次にテストします。

q = (Vehicle.all()
    .filter('production_years =', 2010))
于 2013-02-27T18:35:06.353 に答える