python - model and instance methods: session aware (sqlalchemy) -
so, using , learning sqlalchemy.
i have instance, need value. if value exists, return it. if not calculate , return it.
invariably 'you're doing wrong' , input on improvement appreciated in general. looking how can without explicitly having manage session, because i'm working on starting grow, , managing session when want update instance problematic. makes me think am in fact doing wrong.
so how fix method below not have manage session explicitly?
def method(self, session): if self.i_needed_this none: self.i_needed_this = calculate(calcutron) session.add(self) session.commit() return self.i_needed_this else: return self.i_needed_this maybe question should titled 'making instances session aware i'm not explicitly managing it', , if it's dumb question, @ least show me why examples , point me others have asked better.
edit: apparently, importing session i'm using works, , available, maybe non issue or future 1 when more skilled sqlalchemy.
your question common, answer in session asked questions of sa documentation:
- how can session object? use object_session() classmethod available on session:
session = session.object_session(someobject)
however, assumes object persistent , either new (but added session already) or bound session. sample code contains logic add object session, assumption might not true in case.
Comments
Post a Comment