Just a little detail...
To get the class of a "regular" relationship, I use a class_mapper:
def getClassOfRelationship(cls, name):
retval = None
mapper = sqlalchemy.orm.class_mapper(cls)
try:
prop = mapper.get_property(name)
if isinstance(prop, sqlalchemy.orm.properties.SynonymProperty):
retval = getClassOfRelationship(cls, prop.name)
elif isinstance(prop,
sqlalchemy.orm.properties.RelationshipProperty):
retval = prop.mapper.class_
except ValueError:
retval = None
return retval
But the AssociationProxies don't (seem to) show in the mapper, correct?
I can always find a workaround, like "ok, if the attribute is not in
the class_mapper, then go to the class and check if it's an
AssociationProxy", but I was wondering if there's a better ("cleaner")
way. As AssociationProxies don't show in the class_mapper, I'll be
getting an exception when I try to get it from the mapper (here: prop
= mapper.get_property(name)) If I get an exception, I can go to the
class itself (cls parameter), check whether it's an AssociationProxy,
and then do what it is described in Michael's solution, but maybe
someone knows a cleaner way?
Thank you in advance!
Thanks in advance!
2011/12/7 Michael Bayer <[email protected]>:
>
> On Dec 7, 2011, at 7:53 PM, Hector Blanco wrote:
>
>> Hello everyone:
>>
>> In one of my classes, I am using an associationproxy to provide a list
>> in which the same item can be inserted twice. Basically, a product can
>> contain "images" (several times the same image). One product can have
>> two different lists of "images" (images1 and images2... yep... not
>> very creative with the naming here)
>>
>> Is there a way, given a class Product (or an instance, but preferably a
>> class) knowing that Product._images1 (or Product._images2) is going to
>> give me "Image"s (er... things of class "Image"). I've been able to do
>> that with regular relationships through [relationship].mapper.class_.
>
>
>
> associationproxy has an attribute target_class for the "middle" class:
>
> MyClass.my_association.target_class
>
> then for the target, 0.7 has "remote_attr". Not in 0.6 but it's just
> shorthand for:
>
> getattr(MyClass.my_association.target_class, self.value_attr)
>
> but AP doesn't know what type that is. Suppose it were a relationship, then
> you'd say:
>
> getattr(MyClass.my_association.target_class,
> MyClass.my_association.value_attr).property.class_
>
> if a column:
>
> getattr(MyClass.my_association.target_class,
> MyClass.my_association.value_attr).property.columns[0]
>
> etc.
>
>
> --
> 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.
>
--
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.