霖同学
霖同学

Reputation: 33

sqlalchemy can't get data with boolean field in filter option (specified with is)

Channel.query.filter(Channel.is_default == True).all()

When I use above expression it works,but an pep8 advice is occuered.So I use below expression instead.

Channel.query.filter(Channel.is_default is True).all()

But when I run the code, no data returned.And there is no error or warning.

Upvotes: 3

Views: 3212

Answers (1)

Julio Cezar Silva
Julio Cezar Silva

Reputation: 2456

~When you're dealing with a property or method that returns boolean values, there is absolutely no need to compare it with True or False. It's a bad practice, even.

So, to solve both your PEP8 problem, and your filtering one, just omit the comparison, since Channel.is_default will already yield True or False individually:

Channel.query.filter(Channel.is_default).all()

And if that filter doesn't give you any results, it's because not all within your Channel have is_default set to True. This filter is as straightforward as they come.

Edit: my answer was misinformed, the filter function in sqlalchemy is a very particular case of conditionality in Python. See this question and its most prominent answers, which also answer OP's question as of now.

Upvotes: 2

Related Questions