[flask]出错时发送异常信息邮件(包含异常堆栈,request 信息,发生异常时的局部变量)

Date Category flask

最终效果(邮件内容):

Message type:       ERROR
Location:           /home/xxx/site-packages/flask_restful/__init__.py:292
Module:             __init__
Function:           handle_error
Time:               2014-12-24 17:59:47,876

Message:

Internal Error


Traceback (most recent call last):
  File "/home/xxx/site-packages/flask/app.py", line 1475, in full_dispatch_request
    rv = self.dispatch_request()
  ....
  File "/home/xx/foo.py ...
more ...


[python] 解决 SQLAlchemy 提示 Instance is not bound to a Session 错误的问题

Date Category python

在使用 SQLAlchemy 的过程中,有时会出现下列错误:

  • Parent instance '<User at 0x2b45b53509d0>' is not bound to a Session; lazy load operation of attribute cannot proceed
  • Instance '<User at 0x2b45b53509d0>' is not bound to a Session; attribute refresh operation cannot proceed

出现以上错误的原因是因为:session 已经被提交,导致操作的 model 对象已经不在当前 session 中了。 解决的办法就是:把对象重新加入到当前 session 中:

def ...
more ...