> i.e. the fact that "attr2" points to a dict means that its an object- 
> holding attribute, with its own attributes.

Indeed.  However, I ran in to the case where, eg, 'attr2' points to a dict
but the corresponding attribute on the object mapper is None, in which case
I wanted to create a new instance of the correct type.

> a better choice would be property.mapper.class_ , i.e.
>
>   object_mapper(ormvar).properties[attr].mapper.class_

Out of curiosity, why is this a better choice?  Less likely to change in the
future, or simply more descriptive?

Thanks!

- Cory Johns


-----Original Message-----
From: sqlalchemy@googlegroups.com [mailto:[EMAIL PROTECTED]
Behalf Of Michael Bayer
Sent: Monday, May 07, 2007 3:01 PM
To: sqlalchemy@googlegroups.com
Subject: [sqlalchemy] Re: Determining class of ORM attribute at run-time




On May 7, 2007, at 2:31 PM, Cory Johns wrote:

>
> I'm creating a general routine that is intended to take an  
> arbitrary dict of
> attribute / value pairs and an ORM object instance, and use the  
> dict to
> update the properties of the ORM instance, without knowing a priori  
> whether
> the attributes might be relations to other tables.  The problem I  
> ran in to
> is when a property that is a relation needs to be created anew,  
> because then
> I don't know what class to create an instance of.  From looking at the
> source, I found what I was looking for under the argument attribute  
> of the
> PropertyLoader class, so I am currently doing along the lines of:
>
>   attr_class = object_mapper(ormvar).properties[attr].argument
>   new_instance = attr_class()
>   setattr(ormvar, attr, new_instance)
>
> However, this feels like it might be a bit fragile, as the argument
> attribute isn't explicitly document that I could find.  Is there an  
> API /
> documented way of determining the class that represents a particular
> attribute, as determined by mapper() or relation()?

a better choice would be property.mapper.class_ , i.e.

  object_mapper(ormvar).properties[attr].mapper.class_

but not every entry inside of 'properties' is a PropertyLoader,  
theres also column- and synonym- based properties (and someday may be  
others).

i would think that a dict-based method of updating object attributes  
would itself be "shaped" the same way as an object graph, and you  
wouldnt have to know about classes/attribute types explicitly ?  i.e.

{
        'attr1': 'somevalue',
        'attr2': {
                'subattr1':'somevalue'
        }
}

i.e. the fact that "attr2" points to a dict means that its an object- 
holding attribute, with its own attributes.







CONFIDENTIAL NOTICE: This email including any attachments, contains 
confidential information belonging to the sender. It may also be 
privileged or otherwise protected by work product immunity or other 
legal rules. This information is intended only for the use of the 
individual or entity named above.  If you are not the intended 
recipient, you are hereby notified that any disclosure, copying, 
distribution or the taking of any action in reliance on the contents 
of this emailed information is strictly prohibited.  If you have 
received this email in error, please immediately notify us by 
reply email of the error and then delete this email immediately.

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to