[orm-devel] Access to attributes before a query
Mike Watkins
mw@mikewatkins.net
Sat, 16 Nov 2002 07:48:23 -0800 (PST)
Diedrich - I'm having fun with ORM, currently migrating a web / forms
intensive application over. Prior to ORM I had a simple O-R scheme of my
own that allowed me to do something like this:
if group_id:
g = ds.select(Groups, group_id).fetchone()
# else create an instance
else:
g = Groups()
# populate a form with instance defaults or data if editing an existing
record
form.add_widget('hidden', 'id' , value=group_id)
form.add_widget("string", "code", title="Code", value=g.code, size=20,
required=1)
self.add_widget("string", "name", title="Group name", value=g.name,
size=20, required=1)
self.add_widget("checkbox", "is_login_allowed",
value=num_bool(g.is_login_allowed), title="Allow login?")
However, new dbclass object instances do not have attributes matching
the column names until after an insert / select or when explicitly
initialized. i.e.
g = Groups()
g.name - will raise AttributeError
I think (bear in mind I am still a relative Python newbie) that this
kind of usage is more or less consistant with how someone might expect a
persistence scheme to work.
What do you think?