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.
>>
>>
>>

-- 



Reply via email to