> class Product(SQLObject):
> productName = StringCol(alternateID = True)
> def _get_urlFilename(self):
> return self.productName + '.html'
> Now, how do I select a product from the database by
> urlFilename? I want something like:
> Product.select(Product.q.urlFilename == 'filename')
The select method makes some SQL and sends it to the database. Since your property doesn't add anything to the database, the database will not know anything about urlFilenames. If you were to ::
fooProduct = list(Product.select(Product.q.productName == 'foo'))
then fooProduct.urlFilename would then equal 'foo.html'; but only once the data is gotten from the database and stuffed into a Product object can you talk about urlFilenames.
Given this example, it seems easiest if you want to search by urlFilename to first figure out the productName from it, then query on the productName. You could go like so:
productName = StringCol(alternateID = True)
return self.productName + '.html'
def selectByUrlFilename(cls, filename):
productName = filename[0:-5]
return cls.select(cls.q.productName == productName)
selectByUrlFileName = classmethod(selectByUrlFilename)
fooProduct = Product.selectByUrlFilename('foo.html')
Then you're only storing 'foo' once in the database, so no coherency problems. (selectByUrlFilename should check more things about the filename before blindly hacking off the last five characters, but you get the idea.)