On Fri, Dec 14, 2018 at 4:58 PM Daniel Leon <[email protected]> wrote:
>
> I want to be able to refresh the object immediately before setting its
> attribute and commit immediately after. I notice that the set event is before
> set. Is there an after set event?
there's not an "after" event but you can skip all these events
entirely and override __setattr__() if you wanted to, that's still
"normal" here:
from sqlalchemy import Integer, Column, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import object_session, Session
Base = declarative_base()
class SetHandler(object):
def __setattr__(self, key, value):
print("before set: %s" % value)
super(SetHandler, self).__setattr__(key, value)
print("after set: %s" % value)
sess = object_session(self)
if sess is not None:
print("flushing...")
sess.flush() # this would be a little nutty to do
# for every attribute change but it's possible
class A(SetHandler, Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
a = Column(Integer)
b = Column(Integer)
c = Column(Integer)
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
a1 = A(a=1, b=2, c=3)
s = Session(e)
s.add(a1)
a1.b = 5
a1.c = 10
s.commit()
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.