Thread: [SQLObject] Assigning to a join
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Jeff W. <je...@me...> - 2005-10-22 00:45:16
|
Let's say I have the following objects: class Foo(SQLObject): bars= RelatedJoin("Bar") class Bar(SQLObject): foos= RelatedJoin("Foo") Then later I have: bunchOfFoos= [ Foo(), Foo(), Foo() ] bar= Bar() bar.foos= bunchOfFoos Is there any way in SQLObject I can make this work as one would expect? -- Jeff Watkins http://metrocat.org/ "Computers are like Old Testament gods; lots of rules and no mercy." -- Joseph Campbell |
From: Rene D. <re...@gm...> - 2005-10-22 02:25:53
|
ooh. That would be a really nice way to add related stuff. Very montish. On 10/22/05, Jeff Watkins <je...@me...> wrote: > Let's say I have the following objects: > > class Foo(SQLObject): > bars=3D RelatedJoin("Bar") > > class Bar(SQLObject): > foos=3D RelatedJoin("Foo") > > > Then later I have: > > bunchOfFoos=3D [ Foo(), Foo(), Foo() ] > bar=3D Bar() > bar.foos=3D bunchOfFoos > > Is there any way in SQLObject I can make this work as one would expect? > > -- > Jeff Watkins > http://metrocat.org/ > > "Computers are like Old Testament gods; lots of rules and no mercy." > -- Joseph Campbell > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: > Power Architecture Resource Center: Free content, downloads, discussions, > and more. http://solutions.newsforge.com/ibmarch.tmpl > _______________________________________________ > sqlobject-discuss mailing list > sql...@li... > https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss > |
From: Kevin D. <da...@gm...> - 2005-10-22 02:41:04
|
On 10/21/05, Jeff Watkins <je...@me...> wrote: > Then later I have: > > bunchOfFoos=3D [ Foo(), Foo(), Foo() ] > bar=3D Bar() > bar.foos=3D bunchOfFoos > > Is there any way in SQLObject I can make this work as one would expect? I don't think so. I think you can call bar.addFoo(Foo()), but I don't believe you can set the whole list. I've never seen anyone refer to this, but I've also never tried it. Kevin |
From: Oleg B. <ph...@ph...> - 2005-10-22 15:38:55
|
On Fri, Oct 21, 2005 at 08:45:12PM -0400, Jeff Watkins wrote: > class Foo(SQLObject): > bars= RelatedJoin("Bar") > > class Bar(SQLObject): > foos= RelatedJoin("Foo") > > > Then later I have: > > bunchOfFoos= [ Foo(), Foo(), Foo() ] > bar= Bar() > > Is there any way in SQLObject I can make this work as one would expect? for foo in bunchOfFoos: bar.addFoo(foo) Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: Luke O. <lu...@me...> - 2005-10-22 17:04:19
|
Somewhat depends on whether you expect it to remove any existing relationships and replace them with the assigned list, or just add to the list class Bar(SQLObject): ... def _set_foos(self, val): #If replacement: for x in self.foos: self.removeFoo(x) #Probably ought to check that val is iterable # Add assigned items: for x in val: self.addFoo(x) But you get the idea. You could also be smarter about not removing/adding if one of the assigned values is already in self.foos. Quoting Jeff Watkins <je...@me...>: > Let's say I have the following objects: > > class Foo(SQLObject): > bars= RelatedJoin("Bar") > > class Bar(SQLObject): > foos= RelatedJoin("Foo") > > > Then later I have: > > bunchOfFoos= [ Foo(), Foo(), Foo() ] > bar= Bar() > bar.foos= bunchOfFoos > > Is there any way in SQLObject I can make this work as one would expect? > > -- > Jeff Watkins > http://metrocat.org/ > > "Computers are like Old Testament gods; lots of rules and no mercy." > -- Joseph Campbell > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by: > Power Architecture Resource Center: Free content, downloads, discussions, > and more. http://solutions.newsforge.com/ibmarch.tmpl > _______________________________________________ > sqlobject-discuss mailing list > sql...@li... > https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss > -- The Pursuit of Counterfactual Histories |
From: Jeff W. <je...@me...> - 2005-10-22 20:23:10
|
Thanks Luke, that was much closer to what I was looking for than Oleg's suggestion. However, my ultimate question is how to do this when I don't know *which* join I'm operating on. I want to have something like: class BaseObject(SQLObject): def setJoin( self, join, values ): join_info= self.some_mystical_function_to_find_the_join_info ( join ) # do what Luke described... On 22 Oct, 2005, at 1:04 pm, Luke Opperman wrote: > class Bar(SQLObject): > ... > def _set_foos(self, val): > #If replacement: > for x in self.foos: > self.removeFoo(x) > #Probably ought to check that val is iterable > # Add assigned items: > for x in val: > self.addFoo(x) -- Jeff Watkins http://metrocat.org/ "Just because you have the right to do something, doesn't mean it's the right thing to do." -- Fred Friendly, former president of CBS News |
From: Luke O. <lu...@me...> - 2005-10-23 16:27:04
|
If I were to do this generically, I'd probably look at overriding sqlmeta.addJoin to create the setter (completely untested and somewhat hackish): from sqlobject.main import setterName, rawSetterName from sqlobject import sqlmeta as sm class BaseObject(SQLObject): class sqlmeta(sm): def addJoin(cls, joinDef): soClass = cls.soClass join = joinDef.withClass(soClass) if hasattr(join, 'add') and hasattr(join, 'remove'): meth = join.joinMethodName def func(s, vals, name=meth,arName=join.addRemoveName): # Some getattr ugliness here to do what I wrote before: for x in getattr(s,name): getattr(s,'remove'+arName)(s,x) for x in vals: getattr(s,'add'+arName)(s,x) setattr(soClass, rawSetterName(meth), func) if not hasattr(soClass, setterName(meth)): setattr(soClass, setterName(meth), func) return sm.addJoin(cls, joinDef) addJoin = classmethod(addJoin) In short, define the generic function, and rely on the eventual call to makeProperties to create the set property same as the internal guts of SO do it. If it were to be worked into the main sqlmeta/joins it could be made prettier by modifying RelatedJoin and writing the setJoin method there: def setJoin(self, inst, vals): for x in self.performJoin(inst): self.remove(inst, x) for x in vals: self.add(inst, x) And then the override in sqlmeta would just be if hasattr(join, 'setJoin'), do something similar to the performJoin lamda call. - Luke Quoting Jeff Watkins <je...@me...>: > Thanks Luke, that was much closer to what I was looking for than > Oleg's suggestion. However, my ultimate question is how to do this > when I don't know *which* join I'm operating on. > > I want to have something like: > > class BaseObject(SQLObject): > > def setJoin( self, join, values ): > join_info= self.some_mystical_function_to_find_the_join_info > ( join ) > # do what Luke described... > |