I've finally came up with this following your inspiration @ #1:

class Article(SQLObject):
     title = StringCol()
     photos = MultipleJoin("ArticlePhoto", joinColumn="article_id")

     def _get_photos(self):
         return [ap.photo for ap in self._SO_get_photos()]

     def addPhoto(self, photo, sort_order=0):
         ArticlePhoto(article=self, photo=photo, sort_order=sort_order)

     def removePhoto(self, photo, sort_order=None):
         if sort_order is None:
             aps = ArticlePhoto.selectBy(article=self, photo=photo)
         else:
             aps = ArticlePhoto.selectBy(article=self,  
photo=photo,                                         
sort_order=sort_order)
         for ap in aps: ap.destroySelf()

class Photo(SQLObject):
     title = StringCol()
     articles = MultipleJoin("ArticlePhoto", joinColumn="photo_id")

     def _get_articles(self):
         return [ap.article for ap in self._SO_get_articles()]

     def addArticle(self, article, sort_order=0):
         ArticlePhoto(article=article, photo=self,  
sort_order=sort_order)

     def removeArticle(self, article, sort_order=None):
         if sort_order is None:
             aps = ArticlePhoto.selectBy(article=article, photo=self)
         else:
             aps = ArticlePhoto.selectBy(article=article, photo=self,
                                         sort_order=sort_order)
         for ap in aps: ap.destroySelf()


class ArticlePhoto(SQLObject):
     class sqlmeta:
         defaultOrder = ["sort_order"]
     article = ForeignKey("Article", cascade=True)
     photo = ForeignKey("Photo", cascade=True)
     sort_order = IntCol(default=0)

This way it more resembles the current RelatedJoin interface (with  
addX, removeX, and Xs).
Now I just need to find a way to factor this out for easy reuse, but  
that's my homework... :)

Thanks for the head-start!

Regards,
Alberto




On Apr 9, 2006, at 10:20 PM, isaac wrote:

>
> I've run into this problem too... Here are 2 approaches:
>
> #1 is an example of a model for an ordered series of articles (it
> should be easy to adapt for an ordered list of nearly anything). This
> one is a lot like a RelatedJoin, it just lacks the name.
>
> So far this is how I handle the problem in my apps (anybody have a
> better way?).
>
> class Article(SQLObject):
>      article_title = UnicodeCol()
>      body = UnicodeCol()
>      # author, etc. snipped...
>      # series_item is a MultipleJoin in case some articles
>      # go into more than one series
>      series_item = MultipleJoin('SeriesItem', joinColumn =  
> 'article_id')
>
> class Series(SQLObject):
>      series_title = UnicodeCol()
>      # other metadata fields about your series here...
>      items = MultipleJoin('SeriesItem', joinColumn = 'parent_id')
>
> class SeriesItem(SQLObject):
>      parent = ForeignKey('Series', cascade = True)
>      article = ForeignKey('Article', cascade = True)
>      sort_order = IntCol(default = 0)
>
> The SeriesItem class ends up acting as your intermediate table.
>
> You set the sort_order field in SeriesItem to determine the order. It
> takes a little getting used to, but I find this works for many
> situations, and you can get really clever and re-use the basic
> structure if you use inheritance (make a generic ordered list class,
> and then extend it as needed).
>
> Then the quickest way to your sorted list of items is to get the id
> for the Series you want, then do something like:
>
> stuff = SeriesItem.select(SeriesItem.q.parentID == my_series_id,
> orderBy='sort_order')
>
> #2 -----------------------------
>
> Another approach *might* be to make your intermediate table explicit
> by carefully choosing its name and specifying it on both related
> classes, based on how SQLObject converts names. Using the orderBy arg
> to RelatedJoin, you get automatic sorting.
>
> I've not tested this one, so please post what happens if you try it.
>
> class Apples(SQLObject):
>      fruit = StringCol()
>      orange = RelatedJoin('Oranges', joinColumn='apple_id',
>          otherColumn='orange_id', intermediateTable='apples_oranges',
>          orderBy='sort_order')
>
> class Oranges(SQLObject):
>      fruit = StringCol()
>      apple = RelatedJoin('Apples', joinColumn='orange_id',
>          otherColumn='apple_id', intermediateTable='apples_oranges',
>          orderBy='sort_order')
>
> class ApplesOranges(SQLObject):
>      apple = ForeignKey('Apple', cascade = True)
>      orange = ForeignKey('Orange', cascade = True)
>      sortOrder = IntCol()
>
> Then you can probably set the order column with something like:
>
> my_apple.orange[3].sortOrder = 2
>
> Which way is better probably depends on the data you're working with,
> and how you need  to mess with it.
>
> HTH.
> --isaac
>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears
-~----------~----~----~----~------~----~------~--~---

Reply via email to