Re: [SQLObject] Reimplements the constructor...
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Luke O. <lu...@me...> - 2004-07-20 15:52:15
|
> I want to reimplement the constructor to know when I adding the article > in a section or in a title, so, if the user adding in a section, I can > get the title from the section, the article will always have a title, > even if it is in a section... > > Is it a good idea? Thanks for any help and sorry about my poor english :) I may be misunderstanding you, is article.title a ForeignKey to title that may be NULL (when the title comes from article.section)? If so, from a database perspective I would not duplicate the data to set article.title = article.section.title. If section.title changes, then you need more application code to correctly update the article title, etc. Instead, transparently retrieve the section's title if the article has no title: class Article(sqlobject): title = ForeignKey('Title', default=None) section = ForeignKey('Section', default=None) .. def _get_title(self): storedTitle = self._SO_get_title() if storedTitle: return storedTitle else if self.section: return self.section.title else: return None a = Article(section=aSection, ...) a.title # returns aSection.title a.title = aTitle a.title # returns aTitle Logic may not be exactly how you need it (which has precedence if an article has both a title and a section?), but I would generally solve this in this way: leave the data model clean and get the behavior I want by overriding the properties. (See http://www.sqlobject.org/docs/SQLObject.html#overriding-column-attributes if the _get_columnName thing is unfamiliar.) - Luke |