AttributeError: 'bool' object has no attribute 'clone' in Peewee with SQLite

StackOverflow https://stackoverflow.com/questions/20646814

  •  19-09-2022
  •  | 
  •  

Вопрос

I am trying to get the following query:

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

This does not give me any errors, but any of the following:

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

gives me the following error:

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

If I try this:

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

it will tell me that:

    TypeError: argument of type 'Expression' is not iterable

I find this very confusing, because this:

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

works perfectly fine, while it is very analogous to what I'm trying to do, it seems.

I have absolutely no idea what this might mean and I can't find anything in the documentation. 'bool' object probably stands for boolean, but I cannot see how the result of my query is a boolean. I also don't know what 'clone' means and I also don't know how to resolve this error.

Это было полезно?

Решение

Python casts whatever __contains__() returns to a boolean. That is why you cannot use "not in" or "in" when constructing peewee queries. You instead use << to signify "IN".

You might try:

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

See the docs on querying for more info http://peewee.readthedocs.org/en/latest/peewee/querying.html#column-lookups

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top