I know that, using SQLAlchemy's session
object I can call session.dirty
to get all objects with changes that need to be committed to the database. But how can I determine which specific fields on any one object are dirty? Is there a method I can call on the specific object that returns these fields? Or maybe I can pass the object into one of session
's class methods to get this?
An example from the History
class:
from sqlalchemy import inspect
hist = inspect(myobject).attrs.myattribute.history
If you wanted to check all defined attributes for a given object instance, you probably could do something like this:
def print_changes(myobj):
from sqlalchemy.orm import class_mapper
from sqlalchemy import inspect
inspr = inspect(myobj)
attrs = class_mapper(myobj.__class__).column_attrs # exclude relationships
for attr in attrs:
hist = getattr(inspr.attrs, attr.key).history
print(attr.key, hist.has_changes())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With