On Sep 24, 2012, at 11:16 AM, John Anderson wrote:

> If I have a DB structure similar to this:
> 
> class Parent(Base):
>    pass
> 
> class Child(Base):
>    parent = relation(Parent, backref='children')
> 
> 
> and I have an instanced of Parent and want to figure out what the
> class of instance.children is, how would I do that?

by "class of instance.children", I assume you're looking for "Child", not the 
type of collection (set, list) and not some specific subtype of objects within 
the collection (since there could be many).

So this is a class-level attribute, given Parent.   If you have an instance of 
Parent, and you don't know that it's Parent, you'd call type(my_instance) to 
get the class first.  Assuming we are that far:

up through 0.7:

property = Parent.children.property
target_class = property.mapper.class_

in 0.8, the above, or also:

from sqlalchemy import inspect
property = inspect(Parent.children)
target_class = property.mapper.class_




> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> To unsubscribe from this group, send email to 
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at 
> http://groups.google.com/group/sqlalchemy?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to