I am trying to setup my model initialization in two different ways.
1.) Initialize normally by creating an instance, e.g.,
test0=Test0('Title')
2.) Initialize by query, using an input integer as the primary key,
e.g., test0=Test(32)
This posses two questions. Is it common practice to put a session
inside the model? Google Code search seems to say no, but I don't see
any other option. When I query inside the model, the attributes show
up inside the class but not outside of it. How do I fix this? I have
included some example code and output.
Thanks for your help.
~Grant
== CODE ==
class Test0(Base):
__tablename__ = 'test0'
id = Column(Integer, primary_key=True)
title = Column(String)
session=Session()
def __init__(self, *args):
if type(args[0])==int:
test=self.session.query(Test0).get(args[0])
self=self.session.merge(test)
self.session.add(self)
elif type(args[0])==str:
self.title=args[0]
self.session.add(self)
def commit(self):
self.session.commit()
test0a=Test0('This is a test.')
test0a.commit()
test0b=Test0(test0a.id)
print 'test0a title=' + str(test0a.title)
print 'test0b title=' + str(test0b.title)
test0b.title='CHANGED'
test0b.commit()
== OUTPUT ==
test0a title=This is a test.
test0b title=None=
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.