Flask SQLAlchemy 使用“不等于"查询列 [英] Flask SQLAlchemy querying a column with "not equals"

查看:74
本文介绍了Flask SQLAlchemy 使用“不等于"查询列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以查询我的 Seat 表中没有分配邀请的所有座位:

seats = Seat.query.filter_by(invite=None).all()

然而,当查询所有分配了邀请的席位时,我得到一个NameError:

seats = Seat.query.filter_by(invite!=None).all()

NameError: name 'invite' 未定义

这是我的Seat课程:

class Seat(db.Model):id = db.Column(db.Integer, primary_key=True)邀请 ID = db.Column(db.Integer, db.ForeignKey('invite.id'))邀请 = db.relationship('邀请',backref=db.backref('伙计',lazy='dynamic'))

如何查询所有者不为空的所有座位?

解决方案

filter_by() 方法采用关键字参数序列,因此您必须始终将 = 与它一起使用.

您想使用 filter() 允许 != 的方法:

seats = Seat.query.filter(Seat.invite != None).all()

I can query my Seat table for all seats where there is no invite assigned:

seats = Seat.query.filter_by(invite=None).all()

However, when querying for all seats that have an invite assigned, I get a NameError:

seats = Seat.query.filter_by(invite!=None).all()

NameError: name 'invite' is not defined

Here is my Seat class:

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?

解决方案

The filter_by() method takes a sequence of keyword arguments, so you always have to use = with it.

You want to use the filter() method which allows for !=:

seats = Seat.query.filter(Seat.invite != None).all()

这篇关于Flask SQLAlchemy 使用“不等于"查询列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆