On Oct 20, 2010, at 3:38 AM, Aydın ŞEN wrote:

> I defined my tables below as declarative
> 
> class MyTable(Base):
>     __tablename__ = 'mytable'
>     id = Column(Integer,primary_key = True)
>     title = Column(String(200))    
>     description = Column(String(200))
>     dt_st = Column(Date, default=func.current_date())
>     dt_fn = Column(Date, default=func.current_date())
>     content = Column(Unicode)
>     bla bla bla
> 
> I have a dictionary post data which includes update values, let it be:
> 
> myDict = {"title": "newTitle", "content": "newContent"}
> 
> myDict values are arbitrary so i want to update MyTable with only keys and 
> values in myDict (not write one by one table fields). What is the elegant way 
> to do this?

query has an update():

query(MyTable).filter(MyTable.id==5).update(myDict)

otherwise:

for k in myDict:
    setattr(some_object, k, myDict[k])
Session.commit()

> 
> My second question is about relation, lets add this relation definition to my 
> table definition:
> 
>     categories = relation("Category", order_by="Category.id", 
> backref="MyTable")
> 
>     class Category(Base):
>         __tablename__ = 'category'
>         id = Column(Integer, primary_key = True)
>         title = Column(String(200))          
>         description = Column(String(200))
> 
> How can i update categories when i update MyTable?

same techniques, update myDict with {'categories':[x, y, z]} 


>     
> 
> -- 
> Aydın Şen
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@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 sqlalch...@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