Yes, it works, :-)
Thank you Simon,
j
King Simon-NFHD78 wrote:
Jo wrote:
Thank you for replay my question, Simon,
but I can't find the 'original' state there.
j
Here's an example that shows the results of the get_history function:
#--------
import sqlalchemy as sa
import sqlalchemy.orm as saorm
import sqlalchemy.ext.declarative as decl
from sqlalchemy.orm.attributes import get_history
Base = decl.declarative_base()
class User(Base):
__tablename__ = 'user'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String)
def display_history(prefix, obj, attr):
history = get_history(obj, attr)
print '%16s: added=%r, unchanged=%r, deleted=%r' % (prefix,
history.added,
history.unchanged,
history.deleted)
engine = sa.create_engine('sqlite://')
Base.metadata.create_all(bind=engine)
Session = saorm.sessionmaker(bind=engine)
sess = Session()
u = User(name='jose')
sess.add(u)
display_history('Before commit', u, 'name')
sess.commit()
u = sess.query(User).get(1)
display_history('After load', u, 'name')
u.name = 'simon'
display_history('Modified once', u, 'name')
u.name = 'sqlalchemy rules'
display_history('Modified twice', u, 'name')
sess.commit()
display_history('After commit', u, 'name')
#--------
And here are the results:
Before commit: added=['jose'], unchanged=(), deleted=()
After load: added=(), unchanged=[u'jose'], deleted=()
Modified once: added=['simon'], unchanged=(), deleted=[u'jose']
Modified twice: added=['sqlalchemy rules'], unchanged=(),
deleted=[u'jose']
After commit: added=(), unchanged=(), deleted=()
So when you change a scalar attribute, the old value appears in the
'deleted' list, and the new value in the 'added' list. I'm not sure
exactly when the unchanged attribute is used - I would guess it normally
contains the value loaded from the database as long as it hasn't been
modified. I expect it is empty in the 'After commit' line because the
instance has been expired.
Hope that helps,
Simon
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.