在SQLAlchemy中使用fliter_by函数传递参数时出错 [英] Getting errors with passing parameters with fliter_by function in SQLAlchemy

查看:194
本文介绍了在SQLAlchemy中使用fliter_by函数传递参数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SQLalchemy中有一个表类:

pre $ class $ STDcodes(db.Model)
id = db.Column(db.Integer,primary_key = True)
stdcode = db.Column(db.Integer,nullable = False)
city = db.Column(db.String(30),nullable = False)
state = db.Column(db.String(30),nullable = False)

$ b def __init __(self,stdcode,city,state):
self.stdcode = stdcode
self.city = city
self.state = state
$ b def __repr __(self):
return'< City {}> ;'。format(self.city)

现在我有一个带有一些城市名称的文本文件。我想在上面的数据库中找到城市,并用别的东西替换它们的状态值(Telangana)
我正在尝试使用下面的代码来做到这一点。

  from flask_hello_world_app import db,STDcodes 
f = open('telanganastd.txt')
lines = f.readlines()
for line ($):
line = b $ b stdcode.state =TELANGANA
db.session.add(stdcode)
db.session.commit()

但它不起作用。
我得到以下错误:

pre code $ traceback最近一次调用最后一次
文件/ home / alan / Desktop / Python books / Numbersindia / replacetelangana.py,第10行,在< module>
stdcode = STDcodes.query.filter_by(STDcodes.city.startswith(line))。first()
TypeError:filter_by()只需要1个参数(给出2个)


解决方案使用过滤器,而不是 filter_by filter_by 过滤器的快捷方式,它将等于操作符应用于关键字args,不能用于其他操作。
$ b

过滤执行操作,例如 User.name =='davidism' STDcodes.city.startswith(line),并将它们应用于查询。 filter_by 是一个快捷键,它使用关键字参数而不是操作,并将其展开为等效的相等操作。所以 filter_by(name ='davidism',superuser = True)相当于 filter(User.name =='davidism',User.superuser)



因此, filter_by 可以很方便但有一定的局限性:在被过滤的主要模型上的列,唯一支持的操作是相等的,所有的操作都是'd在一起,没有办法使用或者


I have the following class for a table in SQLalchemy

class STDcodes(db.Model):
id = db.Column(db.Integer, primary_key=True)
stdcode = db.Column(db.Integer, nullable=False)
city = db.Column(db.String(30), nullable=False)
state = db.Column(db.String(30), nullable=False)


def __init__(self, stdcode, city, state):
    self.stdcode = stdcode
    self.city = city
    self.state = state

def __repr__(self):
    return '<City {}>'.format(self.city)

Now I have a text file with some city names. I want to find the cities in the above database and replace their state value with something else("Telangana") I'm trying to use the following code for doing that.

from flask_hello_world_app import db, STDcodes
f = open('telanganastd.txt')
lines = f.readlines()
for line in lines:
    line=line.replace("\n","")
    print line
    stdcode = STDcodes.query.filter_by(STDcodes.city.startswith(line))
    stdcode.state= "TELANGANA"
    db.session.add(stdcode)
    db.session.commit()

But it doesn't work. I get the following error:

Traceback (most recent call last):
File "/home/alan/Desktop/Python books/Numbersindia/replacetelangana.py", line 10, in <module>
stdcode = STDcodes.query.filter_by(STDcodes.city.startswith(line)).first()
TypeError: filter_by() takes exactly 1 argument (2 given)

解决方案

Use filter, not filter_by. filter_by is a shortcut to filter that applies the equality op to keyword args, you can't use it for other ops.

filter takes operations, such as User.name == 'davidism' or STDcodes.city.startswith(line), and applies them to the query. filter_by is a shortcut that takes keyword arguments instead of operations, and expands them into the equivalent equality operations. So filter_by(name='davidism', superuser=True) is equivalent to filter(User.name == 'davidism', User.superuser).

So filter_by can be convenient but has limitations: all keywords are assumed to be columns on the primary model being filtered, the only operation supported is equality, and all operations are and'd together, there's no way to use or.

这篇关于在SQLAlchemy中使用fliter_by函数传递参数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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