Python Flask SQLAlchemy 查询带有“不等于”的列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16093475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask SQLAlchemy querying a column with "not equals"
提问by SeanPlusPlus
I can query my Seattable for all seats where there is no invite assigned:
我可以在我的Seat桌子上查询没有分配邀请的所有座位:
seats = Seat.query.filter_by(invite=None).all()
However, when querying for all seats that have an invite assigned, I get a NameError:
但是,当查询分配了邀请的所有席位时,我得到NameError:
seats = Seat.query.filter_by(invite!=None).all()
NameError: name 'invite' is not defined
Here is my Seatclass:
这是我的Seat课:
class Seat(db.Model):
id = db.Column(db.Integer, primary_key=True)
invite_id = db.Column(db.Integer, db.ForeignKey('invite.id'))
invite = db.relationship('Invite',
backref=db.backref('folks', lazy='dynamic'))
How can I query for all seats where the owner is not blank?
如何查询所有者不为空的所有座位?
采纳答案by Nathan Villaescusa
The filter_by()method takes a sequence of keyword arguments, so you always have to use =with it.
该filter_by()方法采用一系列关键字参数,因此您必须始终使用=它。
You want to use the filter()method which allows for !=:
您想使用filter()允许!=:
seats = Seat.query.filter(Seat.invite != None).all()
回答by bull90
I think this can help
http://docs.sqlalchemy.org/en/rel_0_9/core/sqlelement.html#sqlalchemy.sql.operators.ColumnOperators.isnot
Is None
无
query.filter(User.name == None)
or alternatively, if pep8/linters are a concernquery.filter(User.name.is_(None))
或者,如果 pep8/linters 是一个问题query.filter(User.name.is_(None))
Is not None
不是没有
query.filter(User.name != None)
or alternatively, if pep8/linters are a concern
query.filter(User.name.isnot(None))
或者,如果 pep8/linters 是一个问题
query.filter(User.name.isnot(None))

