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)
Something like:
------- Product.py-------------
class Product(declarativeBase):
__tablename__ = "products"
_id = Column("id", Integer, primary_key=True)
_model = Column("model", Unicode(128))
_price = Column("price", Float)
_images1 = AssociationProxy('_associationsProductsImages1', '_image',
creator= lambda _image: AssociationProductsImages(_images, 1))
_images2 = AssociationProxy('_associationsProductsImages2', '_image',
creator=lambda _image: AssociationProductsImages(_images, 2))
----------------------------
The AssociationProxy "machinery" is defined in another file like:
--------------------------------------------------------------------------------------
class AssociationProductsImages(declarativeBase):
__tablename__ = "associations_products_images"
_id = Column("id", Integer, primary_key=True)
_type = Column("type", SmallInteger)
_productId = Column("product_id", Integer, ForeignKey("products.id"))
_products1 = relationship("Product",
uselist=False,
backref=backref(
"_associationsProductsImages1",
foreign_keys=[_productId],
primaryjoin="
(AssociationProductsImages._type == 1) &
(Product._id ==
AssociationProductsImages._productId)
",
cascade="all, delete,
delete-orphan")
)
_products2 = relationship("Product",
uselist=False,
backref=backref(
"_associationsProductsImages2",
foreign_keys=[_productId],
primaryjoin="
(
AssociationProductsImages._type == 2) &
(Product._id ==
AssociationProductsImages._productId)
",
cascade="all, delete,
delete-orphan")
)
_imageId = Column("image_id", Integer, ForeignKey("images_table.id"))
_image = relationship("Image", uselist=False)
#The creator:
def __init__(self, image, type):
type = int(type)
if type == 1 or type == 2 or type == 3:
self._image = image
self._type = type
else:
raise ValueError("Unhandable 'type' %s" % type)
--------------------------------------------------------------------------------------
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_.
Something like:
---------------------------------------------------------------------
def getClassOfRelationship(cls, name):
retval = None
mapper = sqlalchemy.orm.class_mapper(cls)
try:
prop = mapper.get_property(name)
if isinstance(prop,
sqlalchemy.orm.properties.RelationshipProperty):
retval = prop.mapper.class_
except ValueError:
retval = None
return retval
---------------------------------------------------------------------
What I'd like is tweaking the function a bit and being able to fill
the ???? in :
---------------------------------------------------------------------
def getClassOfRelationship(cls, name):
retval = None
mapper = sqlalchemy.orm.class_mapper(cls)
try:
prop = mapper.get_property(name)
if isinstance(prop,
sqlalchemy.orm.properties.RelationshipProperty):
retval = prop.mapper.class_
elif isinstance(prop,
sqlalchemy.ext.associationproxy.AssociationProxy):
retval = ?????
except ValueError:
retval = None
return retval
---------------------------------------------------------------------
But I haven't been able to do that (eeer... obviously, otherwise, I
wouldn't be asking)
Thank you in advance!!
--
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.