Why would you want to do this?
Well, one purpose would be to populate WidgetList derivative classes
for preparation to serve an Edit page.
Why do you need this? you can hardcode it, can't you?
Well, If you have a Model that has 100 properties, how do you come up
with a dictionary containing the property name as the key and the
property value as the value for each and every property? Typing the
code would be an invitation to a disaster.
So, we'll do it the dynamic way:
class TreeBark(SQLObject):
property1 = StringCol(length=150, varchar=True,title="Main Picture
URL")
property2 = StringCol(length=100,varchar=True,title="Title or
Short Description",alternateID=True,notNone=True)
def gimmeDict(self):
d = TreeBark.__dict__
props = dict()
for a in d:
if type(d[a]) == property:
props[a] = self.__dict__.get('_SO_val_%s' % a)
props['id'] = self.id
return props
Explanation:
The __dict__ built-in attribute in every Python class definition
contains all of the stuff in the namespace. So we can separate those
whose type is 'property' and use it to build the key part of our
dictionary.
Then to get the actual dictionary, all we have to do is to invoke the
gimmeDict() from an actual instance of the class.
The result would be a nicely populated dictionary like this:
props = {'property1':'value1', 'property2':'value2'}
... ready to be handed to a TableForm widget, etc. to build an Edit
page.
NOTE: This will probably work only on SQLObject based on the _SO_ bit
in the code. But who knows, I never dealt with SQL Alchemy.
Hope this is useful for someone somewhere.
Will
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---