> -----Original Message----- > From: sqlalchemy@googlegroups.com > [mailto:[EMAIL PROTECTED] On Behalf Of Ken Pierce > Sent: 29 September 2007 00:21 > To: sqlalchemy > Subject: [sqlalchemy] one-to-many access and modification > > > Hello all, > > I just got into using sqlalchemy today and I have a question about > accessing data in a one-to-many relationship. > > I've got a simplified example below. Basically the system is for a > group of us voting on DVDs we want to rent. There are users and films > and for each film a user makes a yes / no choice (where ? is > undecided). > > # create tables > users_table = Table('users', metadata, > Column('id', Integer, primary_key=True), > Column('name', String(40)), > mysql_engine='InnodB') > > films_table = Table('films', metadata, > Column('fid', Integer, primary_key=True), > Column('title', String(128)), > mysql_engine='InnodB') > > choices_table = Table('choices', metadata, > Column('fid', Integer, ForeignKey('films.fid'), primary_key=True), > Column('id', Integer, ForeignKey('users.id'), primary_key=True), > Column('choice', MSEnum("'?'","'Y'","'N'")), > mysql_engine='InnodB') > > # classes here > class User(object): > ... > class Film(object): > .... > class Choice(object): > .... > > # mappers > mapper(User, users_table) > mapper(Film, films_table, properties={'choices':relation(Choice)}) > mapper(Choice, choices_table) > > So, if I retrieve a Film object f, f.choices gives me a list of Choice > objects. From that Film object, I want to look up (and possibly > modify) a users choice. > > My question is, is there a way to set up this relationship so that I > could access a users choice like a dictionary (either with a user id > or User object), or do I have to write a method to search the list and > return the object -- in which case, would the changes be reflected in > the session / database? > > Thanks in advance, > Most impressed so far, > > Ken Pierce. >
You may be interested in the 'Custom List Classes' section of the documentation: <http://www.sqlalchemy.org/docs/03/adv_datamapping.html#advdatamapping_p roperties_customlist> If you are using 0.4, the mechanism has changed slightly: <http://www.sqlalchemy.org/docs/04/mappers.html#advdatamapping_relation_ collections> Regardless of whether you do this or not, if you wrote a method that searched the list of choices and returned the appropriate one, the changes should always be reflected in the session. Hope that helps, Simon --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---