问题描述
假设我有这样一个模型:$ p $
class User(db.Model):
id = db.Column(db.Integer,primary_key = True)
hometown = db.Column(db.String(140))
university = db.Column(db.String(140))
要获得纽约用户的列表,这是我的查询:
User.query.filter_by(hometown ='New York')。all()
要得到一个去USC的用户列表,这是我的查询:
$ p $ User.query.filter_by(university ='USC')。all()
来自纽约的用户,以及去USC的用户,这是我的查询:
$ b $ pre $ User.query.filter_by(hometown ='纽约')。filter_by(university ='USC')。all()
现在,喜欢根据变量的值动态生成这些查询。
例如,我的变量可能如下所示:
{ 'hometown':'New York'}
或者像这样:
{'university':'USC'}
...或甚至像这样:
[{'hometown':'New York'},{'大学':'USC'}]
你可以帮我写一个函数, (或字典列表)作为输入,然后动态地生成正确的sqlalchemy查询?
如果我尝试为关键字使用一个变量,我得到这个错误:
key ='university'
User.query.filter_by(key ='USC')。all()
InvalidRequestError:Entity'< class'User'>第二,我不知道如何动态链接多个filter_by表达式。
我可以明确地调用一个filter_by表达式,但是如何根据一个变量链接几个?
希望这样做更有意义。
谢谢!
解决方案 SQLAlchemy的。
因此,允许SQLAlchemy的开发人员以字典形式接收任意一堆关键字参数。但是你所要求的却是相反的:你可以将任意一堆关键字参数传给一个函数吗?
事实证明,在Python您可以使用一个名为解包 。简单地创建参数字典并将其传递给前面有 **
的函数,如下所示:
kwargs = {'hometown':'New York','university':'USC'}
User.query.filter_by(** kwargs)
#相当于说...
User.query.filter_by(hometown ='纽约',university ='USC')
Let's say I have a model like this:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
hometown = db.Column(db.String(140))
university = db.Column(db.String(140))
To get a list of users from New York, this is my query:
User.query.filter_by(hometown='New York').all()
To get a list of users who go to USC, this is my query:
User.query.filter_by(university='USC').all()
And to get a list of users from New York, and who go to USC, this is my query:
User.query.filter_by(hometown='New York').filter_by(university='USC').all()
Now, I would like to dynamically generate these queries based on the value of a variable.
For example, my variable might look like this:
{'hometown': 'New York'}
Or like this:
{'university': 'USC'}
... Or even like this:
[{'hometown': 'New York'}, {'university': 'USC'}]
Can you help me out with writing a function which takes a dictionary (or list of dictionaries) as an input, and then dynamically builds the correct sqlalchemy query?
If I try to use a variable for the keyword, I get this err:
key = 'university'
User.query.filter_by(key='USC').all()
InvalidRequestError: Entity '<class 'User'>' has no property 'key'
Secondly, I am not sure how to chain multiple filter_by expressions together dynamically.
I can explicitly, call out a filter_by expression, but how do I chain several together based on a variable?
Hope this makes more sense.
Thanks!
解决方案 SQLAlchemy's filter_by
takes keyword arguments:
In other words, the function will allow you to give it any keyword parameter. This is why you can use any keyword that you want in your code: SQLAlchemy basically sees the arguments a dictionary of values. See the Python tutorial for more information on keyword arguments.
So that allows the developers of SQLAlchemy to receive an arbitrary bunch of keyword arguments in a dictionary form. But you're asking for the opposite: can you pass an arbitrary bunch of keyword arguments to a function?
It turns out that in Python you can, using a feature called unpacking. Simply create the dictionary of arguments and pass it to the function preceded by **
, like so:
kwargs = {'hometown': 'New York', 'university' : 'USC'}
User.query.filter_by(**kwargs)
# This above line is equivalent to saying...
User.query.filter_by(hometown='New York', university='USC')
这篇关于用关键字作为变量的烧瓶sqlalchemy查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!