2013-12-17 37 views
2

我試圖讓下面的查詢:AttributeError的:「布爾」對象在Peewee沒有屬性「克隆」的SQLite

ignored = Activity.select().join(StuActIgnore).join(Student).where(Student.id == current_user.id) 
Activity.select().where(~(Activity.name**"%BBP") & Activity not in ignored) 

這不會給我任何錯誤,但以下任何一項:

Activity.get(~(Activity.name**"%BBP") & Activity not in ignored) 
Activity.select().where(~(Activity.name**"%BBP") & Activity not in ignored).join(Course) 

給了我以下錯誤:

AttributeError: 'bool' object has no attribute 'clone' 

如果我試試這個:

Activity.select().join(Course).join(StuCouRel).join(Student).where(~(Activity.name**"%BBP") & Activity not in ignored & Student.id == current_user.id) 

它會告訴我說:

TypeError: argument of type 'Expression' is not iterable 

我覺得這是非常令人困惑,因爲這個:

already_selected = Course.select().join(StuCouRel).join(Student).where(Student.id == current_user.id) 
to_choose = Course.select().where(Course not in already_selected) 

工作完全正常,而這是非常類似於我想要看來。

我完全不知道這可能意味着什麼,我在文檔中找不到任何東西。 'bool'對象可能代表布爾值,但我看不到我的查詢結果是一個布爾值。我也不知道'克隆'是什麼意思,我也不知道如何解決這個錯誤。

回答

2

Python將任何形狀的信息投給__contains__()返回一個布爾值。這就是爲什麼在構建peewee查詢時不能使用「not in」或「in」。您改爲使用<<來表示「IN」。

您可以試試:

ignored = (Activity 
      .select() 
      .join(StuActIgnore) 
      .join(Student) 
      .where(Student.id == current_user.id)) 
Activity.select().where(~(Activity.name**"%BBP") & ~(Activity.id << ignored)) 

參見查詢更多信息的文檔http://peewee.readthedocs.org/en/latest/peewee/querying.html#column-lookups

+3

也有一個疑難雜症我傾向於現在,然後打每一個;忘記在等價性檢查中提供模型屬性..即做Account.get(name == name)而不是Account.get(Account.name == name)。 第一段代碼結束查詢「..FROM」帳戶「AS t1 WHERE?[True]」,並且在此處失敗https://github.com/coleifer/peewee/blob/2.6.1/peewee.py #L2252與 「AttributeError:'bool'對象沒有屬性'clone'」 它會很好,如果它會在提醒語法中包裝異常。 :) – stt

+0

@stt照顧做出答案? – grandchild

相關問題