Re: [SQLObject] Objects with localized properties
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Ian B. <ia...@co...> - 2003-05-07 13:28:11
|
On Wed, 2003-05-07 at 04:34, deelan wrote: > p1 = Product(10) > > the object would contain all the possibile names in the implemented > locales? what about a: > > p1 = Product(10, 'it') > p1.name <-- get italian localized name > > to load up just the italian name and description for the product? i > think this would case some issues, since if i do: > > p2 = Product(10, 'en') > > p2 is really the same object in the DB but now contains english name and > description. probably this solution make more sense: > > p1 = Product(10) > p1.setLocale('it') > p1.name <-- get italian name > p1.setLocale('en') > p1.name <-- get english name > > finally i would like to avoid doing this: > > p1 = Product(10) > p1.nameEn <-- return english name > p1.nameIt <-- return italian name > > since when a new locale is added i need to add a new variable. > what are your thoughts? What are you thinking for the database schema? I imagine there's two columns in that case. One possibilty might be to have a nameEn and nameIt, and do to: class Product(SQLObject): ... def _get_name(self): if self.locale == 'en': return self.nameEn elif self.locale == 'it': return self.nameIt def _set_name(self, value): if self.locale == 'en': self.nameEn = value elif self.locale == 'it': self.nameIt = value But, assuming locale gets set by the interface somehow, this makes Person unthreadsafe for multiple users with multiple languages (since typically a single object would be shared among them). Though if you turn caching off there's no reason not to share objects among threads (though you can't do that now). You could also do p1.name('en'), which is probably marginally better than p1.nameEn. You end up programming name() on your own though, which would be tedious though straightforward. Essentially it is a compound field (i.e., a single Python field that has multiple database fields) -- a similar problem perhaps to something like a point object, where the point is made up of an x and y column. Then you might get p1.name['en'] -- i.e., name returns a dictionary, that is created from name_en, name_it, etc. Maybe that would be defined like: class Product(SQLObject): name = DictOfCols({'it': 'name_it', 'en': 'name_en'}) But that's just speculative. Again, without changes you can implement _get_name(self): return {'it': self.nameIt, 'en': self.nameEn}, at least until I feel clearer implementing something specific. Maybe Person(1, 'it') would be possible, at some point, but I'm not sure how I would implement that now. Ian |