Thank you Anthony! That works well!
Just a small question, I am trying to understand web2py better:
I am using 2 other tables to enable the above scenario:
(1) auth_account table to hold the account information
(2) auth_many table for many to many relationship
And when joining both tables and returning the session.account,
myset = db(
(db.auth_account.id==db.auth_many.account_id) &
(db.auth_many.user_id==auth.user_id)
)
session.account = myset.select(db.auth_account.ALL).first()
I get an extra entry called "auth_many" in the session like:
account : auth_many : <gluon.dal.Set object at 0x109f76b10>
Why does this happen (I didn't "select" auth_many table), and how can
it be removed?
session is blind to users right?
On Dec 6, 11:06 pm, Anthony <[email protected]> wrote:
> Presumably session.account is not always defined -- first you have to
> save 'account' to the session, so any request before that happens
> won't include 'account' in the session.
>
> session is a Storage() object, so when you attempt to access keys that
> are not defined, it simply returns None. That's why session.account
> doesn't produce an error, even if 'account' hasn't been defined.
> However, session.account.id produces an error because session.account
> is None, and None does not have an attribute named 'id'.
>
> What do you want the default to be in case there is no 'account' in
> session? Maybe something like:
>
> default=session.account and session.account.id or None
>
> Anthony
>
> On Dec 6, 2:06 am, lyn2py <[email protected]> wrote:
>
>
>
>
>
>
>
> > Reference post:
> > Scope authenticated users in
> > accountshttps://groups.google.com/group/web2py/browse_thread/thread/4e2bfa3f4...
>
> > I used fishwebby's method to scope authenticated users. The code used
> > is:
>
> > >> db._request_tenant = 'account_id'
> > >> db._common_fields=[Field('account_id',default=session.account.id,
> > >> writable=False, readable=False)]
>
> > But I get this error:
> > AttributeError: 'NoneType' object has no attribute 'id'
>
> > When I change default to session.account,>>
> > db._common_fields=[Field('account_id',default=session.account,
> > writable=False, readable=False)]
>
> > It doesn't give error, but when I use session.account.id or
> > session.account['id'] the error comes back.
>
> > Because session.account holds all the account info, it is better than
> > just to have the id in session.account.
> > Is this a bug or must I use session.account to hold only the account
> > id?
>
> > Or to solve this another way:
> > How can I create an accessible "account" variable like "auth", so that
> > I can use default=account.id or default=auth.account_id?
>
> > Thanks.