On 25 Aug 2012, at 8:33 AM, Marek Mollin <[email protected]> wrote:
> Hello,
>
> I have a problem with a construct like that:
> db.define_table('test',
> Field('name'),
> )
>
>
> class Test(object):
> def get(row):
> return row.name
> def get_hello(row, greeting):
> return '%s %s' % (greeting, row.name)
>
>
> db.test.get = Field.Lazy(Test.get)
> db.test.get_hello = Field.Lazy(Test.get_hello)
>
> Why it would not work ?
> It works perfect if its a standard function. But if its unbound method I get
> TypeError expected at least 1.
>
> I am trying to do that in order to better organize models, I do not want to
> go as far as Bruno's modelless aproach as performance is not the issue but
> overtime they become messy. (Also I know that I can just make seperate file,
> but then I have to add number to the file and again it becomes very unclean).
>
> The whole deal is to move at least some of the logic to those Lazy fields
> (its a pain to have fat controllers).
>
>
At the very least you'll need to declare get and get_hello to be class (or
static) methods. Remember that 'self' in Python is an arbitrary name; your
class would be syntactically identical if you had written it:
class Test(object):
def get(self):
return self.name
def get_hello(self, greeting):
return '%s %s' % (greeting, self.name)
...and you wouldn't expect that to work as you wanted.
--