Thanks very much! it works!
if I have the user query in the controller like this:
def user_list():
users = db(db.auth_user).select()
entities = db(db.entity.id.belongs(user.entity)).select(db.entity.name)
return dict(users = users, entities = entities )
will work?
---
Another related question is: why this don't work?
entities = db.auth_user.entity.represent(user.entity)
on the book appears as the way to get the work done, but it doesn't
On Wednesday, September 12, 2012 6:32:44 AM UTC-3, Niphlod wrote:
>
> more or less
>
> {{for user in db(db.auth_user).select():}}
> Name : {{=user.name}}
> {{entities = db(db.entity.id.belongs(user.entity)).select(db.entity.
> name)}}
> Entities : {{', '.join(ent.name for ent in entities)}}
> {{pass}}
> --
>
> of course you should probably fetch the result in the controller and show
> only them in the view.
>
> BTW: this is pretty simple and standard for web2py. If you were asking for
> something else please post more details...
>
> Il giorno mercoledì 12 settembre 2012 11:10:33 UTC+2, Pepe Araya ha
> scritto:
>>
>> Hi,
>> a question: how do you display the entities related to a user in a View
>> which displays a list of users with their related entities??
>>
>> for example:
>>
>> Name: user 1
>> Entities: one, two
>>
>> --
>>
>> Name: user 2
>> Entities: one
>>
>> --
>> ...
>>
>> Thanks.
>>
>> On Friday, May 11, 2012 12:16:32 PM UTC-4, Niphlod wrote:
>>>
>>> If you need to fetch all entities knowing the user in advance there is a
>>> ultra-simple way to fetch entities in a single query....
>>> Actually I don't think anyone will come up with a solution involving
>>> more than 1 additional query to the one required for "selecting" the user.
>>>
>>> user = db.auth_user(auth.user_id)
>>> entities = db(db.entity.id.belongs(user.entity)).select(db.entity.name)
>>>
>>> Mind that the auth_user line is copied into session, so if that is
>>> always available you can skip the first query and do
>>>
>>> entities = db(db.entity.id.belongs(auth.user.entity)).select(
>>> db.entity.name)
>>>
>>>
>>>
>>> Il giorno venerdì 11 maggio 2012 17:28:38 UTC+2, weheh ha scritto:
>>>>
>>>> db.define_table('entity', Field('name'), format='%(name)s')
>>>> auth_user_table = db.define_table(
>>>> auth.settings.table_user_name,
>>>> ...
>>>> Field('entity', 'list:reference db.entity',
>>>> requires=IS_IN_DB(db, 'entity.id','%(name)s', multiple=True
>>>> ),
>>>> ...
>>>> )
>>>>
>>>> Later, I want to get a list of entities by name from the list:
>>>> reference entry for the current user.
>>>>
>>>> I would think I could do this:
>>>> user = db(db.auth_user.id == auth.user_id).select().first()
>>>> entities = db.auth_user.entity.represent(user.entity)
>>>> but I get a ticket:
>>>> File "N:/web2py/applications/myapp/controllers/mycontroller.py",line
>>>> 15, in myfunc
>>>> return dict(etext=db.auth_user.entity.represent(user.entity))
>>>> TypeError: 'NoneType' object is not callable
>>>>
>>>>
>>>> I've tried a few different variations on the theme, but none working so
>>>> far. Any help would be appreciated.
>>>>
>>>> There are obvious slower ways to do this, but inelegant. I want the
>>>> fastest, tightest solution.
>>>>
>>>>
>>>>
--