>
> db.define_table('accounts',
>                 Field('name','string'),
>                 Field('parent_account','reference accounts'),
>                 Field('account_type','string'))
>
> What I'm trying to achieve is to create a tree-like structure. So we have 
> 5 "root" accounts, which can each hold some sub-accounts etc.
> Now the account_type must be the same for all the children of a certain 
> account. So the root accounts pre-determine all of the children's account 
> type, rendering it unwritable for a child account.
> For some reason the following didn't work for me:
>
> account_types = ['a','b','c']
> db.accounts.account_type.requires  = IS_IN_SET(account_types)
> db.accounts.account_type.required  = True
> db.accounts.account_type.compute = lambda x: 
> x['parent_account'].account_type
> db.accounts.account_type.writeable = lambda x: (x['parent_account'] == 0)
>

For the "compute" attribute, maybe:

db.accounts.account_type.compute = lambda r: 
db.accounts[r.parent_account].account_type if r.parent_account else None

However, I don't think you can set the "writeable" attribute this way 
because you won't know if a parent account is going to be specified until 
after the new record is submitted, which happens after you have to decide 
whether to make the account_type field writeable. Instead you might need 
some client side JS and Ajax to dynamically set the account_type if the 
user selects a parent_account in the form.

Anthony

Reply via email to