Reputation: 71
I am using code like below from the post of Tracking model changes in SQLAlchemy. The only issue I am currently having is my map gets the end user's username through a token and I need to pass that username info into this function so it is logged with the changes. Is there a better method or do I just need to add a column to my tables called last modified by and just scrape that with this listener on update to the table?
@event.listens_for(cls, 'before_update')
def before_udpate(mapper, connection, target):
state = db.inspect(target)
changes = {}
for attr in state.attrs:
hist = state.get_history(attr.key, True)
if not hist.has_changes():
continue
# hist.deleted holds old value
# hist.added holds new value
changes[attr.key] = hist.added
# now changes map keys to new values
Or is there some type of way that I can call this information from my view?
Upvotes: 2
Views: 554
Reputation: 71
After sleeping on this for the night. I came up with doing a mixin. Similar to what is on this page: http://francesco.pischedda.info/posts/logging-model-changes-with-sqlalchemy-listeners.html
I pass in my request object so that my target object will have the request object.
Upvotes: 1