我有一个案例,大多数情况下,对象之间的关系是这样的,即预先配置对关系的热切(加入)负载是有意义的.但是现在我遇到了一种我真的不希望完成负载的情况.
我应该从关系中删除已加入的加载并更改所有相关查询以在查询位置(ick)加入,还是有某种方法可以在查询设置后抑制查询中的急切加载?
下面是一个示例,其中已在User-> Address关系上设置了预先加载.程序末尾的查询是否可以配置为不急于加载?
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy.orm as orm
##Set up SQLAlchemy for declarative use with Sqlite...
engine = sa.create_engine("sqlite://", echo = True)
DeclarativeBase = declarative_base()
Session = orm.sessionmaker(bind = engine)
class User(DeclarativeBase):
__tablename__ = "users"
id = sa.Column(sa.Integer, primary_key = True, autoincrement = True)
name = sa.Column(sa.String, unique = True)
addresses = orm.relationship("Address",
lazy = "joined", #EAGER LOAD CONFIG IS HERE
)
def __init__(self, Name):
self.name = Name
class Address(DeclarativeBase):
__tablename__ = …Run Code Online (Sandbox Code Playgroud) 假设我engine指向MySQL数据库:
engine = create_engine('mysql://arthurdent:answer42@localhost/dtdb', echo=True)
Run Code Online (Sandbox Code Playgroud)
我可以通过以下方式填充dtdb表格,FK等:
metadata.create_all(engine)
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法来生成包含所有DDL语句的SQL文件,而不是实际应用这些DDL语句dtdb?
到目前为止,我已经采取了捕获SQLAlchemy日志输出echo=True,并手动编辑它.但那太痛苦了.
看起来SA有相当复杂的模式管理API,但我还没有看到简单地将模式定义作为文本流式传输的示例.
我刚刚编写了一个类装饰器,如下所示,试图为目标类中的每个方法添加调试支持:
import unittest
import inspect
def Debug(targetCls):
for name, func in inspect.getmembers(targetCls, inspect.ismethod):
def wrapper(*args, **kwargs):
print ("Start debug support for %s.%s()" % (targetCls.__name__, name));
result = func(*args, **kwargs)
return result
setattr(targetCls, name, wrapper)
return targetCls
@Debug
class MyTestClass:
def TestMethod1(self):
print 'TestMethod1'
def TestMethod2(self):
print 'TestMethod2'
class Test(unittest.TestCase):
def testName(self):
for name, func in inspect.getmembers(MyTestClass, inspect.ismethod):
print name, func
print '~~~~~~~~~~~~~~~~~~~~~~~~~~'
testCls = MyTestClass()
testCls.TestMethod1()
testCls.TestMethod2()
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
Run Code Online (Sandbox Code Playgroud)
运行上面的代码,结果是:
Finding files... …Run Code Online (Sandbox Code Playgroud) 假设我有一个包含以下内容的字节流:
POST /mum/ble?q=huh Content-Length: 18 Content-Type: application/json; charset="utf-8" Host: localhost:80 ["do", "re", "mi"]
有没有办法从中产生WSGI风格的"环境"字典?
希望,我忽略了一个简单的答案,并且它与相反的操作一样容易实现.考虑:
>>> import json
>>> from webob import Request
>>> r = Request.blank('/mum/ble?q=huh')
>>> r.method = 'POST'
>>> r.content_type = 'application/json'
>>> r.charset = 'utf-8'
>>> r.body = json.dumps(['do', 're', 'mi'])
>>> print str(r) # Request's __str__ method gives raw HTTP bytes back!
Run Code Online (Sandbox Code Playgroud)
POST /mum/ble?q=huh Content-Length: 18 Content-Type: application/json; charset="utf-8" Host: localhost:80 ["do", "re", "mi"]
python ×4
sqlalchemy ×2
class ×1
closures ×1
decorator ×1
lazy-loading ×1
scope ×1
webob ×1
wsgi ×1