svilen wrote: > > On Tuesday 11 December 2007 13:13:37 King Simon-NFHD78 wrote: > > Hi, > > > > I used to be able to iterate over mapper.properties.items() to get > > the name of each mapped property along with the object that > > implements it. However, in 0.4.1, trying to do this results in a > > NotImplementedError telling me to use iterate_properties and > > get_property instead, but I can't see a way to get the name of the > > property through either of these methods. > > > > Is there a generic way that, given a mapped class, I can get a list > > of the mapped properties with their names? > iterate_props yields Property instances; maybe p.key is the name u > want.
Ah yes, p.key was exactly what I was looking for - thanks.
The next part of the question is that I am using this in the
after_insert method of a MapperExtension to write a log file with all
the values that were written to the database. The code looks something
like this:
mapper = orm.object_mapper(instance)
for prop in mapper.iterate_properties:
log.info(' %s=%r',
prop.key, getattr(instance, prop.key))
This works nicely for attributes that I set directly. However, it breaks
when it comes across a column that is defined as:
sa.Column('date_created', sa.DateTime,
default=sa.func.current_timestamp(type=sa.DateTime))
The attached script should show the problem. The traceback is:
Traceback (most recent call last):
File "satest.py", line 32, in after_insert
print " %s = %r" % (prop.key, getattr(instance, prop.key))
File
"/home/warranty/python/development/lib/python2.5/site-packages/SQLAlchem
y-0.4.1-py2.5.egg/sqlalchemy/orm/attributes.py", line 40, in __get__
return self.impl.get(obj._state)
File
"/home/warranty/python/development/lib/python2.5/site-packages/SQLAlchem
y-0.4.1-py2.5.egg/sqlalchemy/orm/attributes.py", line 215, in get
value = callable_()
File
"/home/warranty/python/development/lib/python2.5/site-packages/SQLAlchem
y-0.4.1-py2.5.egg/sqlalchemy/orm/attributes.py", line 628, in
__fire_trigger
self.trigger(instance, [k for k in self.expired_attributes if k not
in self.dict])
File
"/home/warranty/python/development/lib/python2.5/site-packages/SQLAlchem
y-0.4.1-py2.5.egg/sqlalchemy/orm/session.py", line 1112, in
load_attributes
if
object_session(instance).query(instance.__class__)._get(instance._instan
ce_key, refresh_instance=instance, only_load_props=attribute_names) is
None:
AttributeError: 'User' object has no attribute '_instance_key'
I assume the problem is that my date_created column isn't immediately
available at the 'after_insert' stage, because it is generated in the
SQL INSERT statement, but hasn't been read back from the database yet.
Is there a more suitable hook-point than after_insert, where I can
safely read values like this?
Thanks a lot,
Simon
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
satest.py
Description: satest.py
