title: How can I get a SQLAlchemy ORM object's previous state after a db update?
body: <p>The issue is that I can't figure out how to use SQLAlchemy to notify me when an object goes into a new state.</p>
  
  <p>I'm using SQLAlchemy ORM (Declarative) to update an object:</p>
  
  <pre><code>class Customer(declarative_base()):
  
      __table_name__ = "customer"
  
      id = Column(Integer, primary_key=True)
      status = Column(String)
  </code></pre>
  
  <p>I want to know when an object enters a state. Particularly after an UPDATE has been issued and when the state changes. E.g. <code>Customer.status == 'registered'</code> and it previously had a different state.</p>
  
  <p>I'm currently doing this with an <code>'set'</code> attribute event:</p>
  
  <pre><code>from sqlalchemy import event
  from model import Customer
  
  def on_set_attribute(target, value, oldvalue, initiator):
      print target.status
      print value
      print oldvalue
  
  event.listen(
          Customer.status,
          'set',
          on_set_attribute,
          propagate=True,
          active_history=True)
  </code></pre>
  
  <p>My code fires every time 'set' is called on that attribute, and I check if the <code>value</code> and the <code>oldvalue</code> are different. The problem is that the <code>target</code> parameter isn't fully formed so it doesn't have all the attribute values populated yet. </p>
  
  <p>Is there a better way to do this? Thanks!</p>