On Jul 25, 2009, at 3:57 AM, robert rottermann wrote:
>
> Michael Bayer schrieb:
>> robert rottermann wrote:
>>> hi there,
>>> how can an instance of a class with a Table bound to it
>>> access the values of the values of "its" db record?
>>>
>>>
>>> what I would like to do is something like the following:
>>> class company(Base):
>>> __table__ = 'tblCompany'
>>> ...
>>>
>>> def listAssignedProducts(self):
>>> print assigned products
>>>
>>>
>>> class tblProducts_kl(Base2):
>>> '''
>>> User item kl class
>>> '''
>>> __tablename__ = 'tblProducts_kl'
>>> companies = relation(
>>> 'tblCompany',
>>> secondary=tables2['tblProductsSupplier_kl'],
>>> backref="related_klProducts",
>>> )
>>>
>>> def assignMyselfToCompany(self, company_id):
>>> # add my self to company
>>> company = getCompanyByIs(company_id)
>>>
>>> now how do I proceed from here?
>>>
>>>
>>
>> why not use query() to find the company you're looking for ?
> thanks
> sorry that I was not clear enough.
>
> I do get company easily (using a query)
> but I want to know the id of the product from within
>
> using the instance it is easy to get at the id like so:
> product_id = tblProducts_kl().id
>
> this product_id I would like to get from within a method of the
> tblProducts_kl
> class definition of so I can tell a tblProducts_kl instance to
> associate itself
> with a company. For this the method assignMyselfToCompany method
> needs to know
> the product_id of the instance for which it is called.
>
> I could do it like this
> class tblProducts_kl(Base2):
> ...
> def assignMyselfToCompany(self, product_id, company_id):
> product = session.query(tblProducts_kl).\
> filter(tblProducts_kl.__table__.c.id ==product_id).all()[0]
> company = session.query(tblCompany).\
> filter(tblCompany.__table__.c.id==company_id).all()[0]
> company.related_klProducts.append(product)
>
> product = tblProducts_kl()
> product.assignMyselfToCompany(product.id, company_id)
>
> however I would like to change the assignMyselfToCompany method in a
> way that I
> could achieve the same with:
>
> product = tblProducts_kl()
> product.assignMyselfToCompany(company_id)
sorry, its not clear to me what it is you're trying to do. assigning
an object to a related object that is m2m is in one of two ways -
either you're using relation()/backref and just saying,
product.companies.append(company), or you'd like to manipulate the
table yourself in which you'd just say,
session.execute(productssupplier.insert(), {'product_id':product.id,
'company_id':company.id})
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---