many to many fields representation

43 views
Skip to first unread message

icodk

unread,
Aug 22, 2015, 7:11:12 AM8/22/15
to web2py-users
I made a form for editing  the auth_membership table using smart grid, it works fine.
The only issue I have is that the id of the user_id and the id of the group_id are appended to the user name and to the group name.
How can I get rid of this 'extra' addition ?
For example the user John Smith is shown as: John Smith(15) both in the create/edit and in the grid 
The group named Teachers is shown as Teachers(4) 

Anthony

unread,
Aug 22, 2015, 11:33:15 AM8/22/15
to web2py-users
By default, the "represent" attribute of a reference field is based on the "format" attribute of the table it references. So, you have two options -- you could either change the "format" attributes of the auth_user and auth_group tables:

db.auth_user._format = '%(first_name)s %(last_name)s'  # notice the "id" field is excluded

or you could directly change the "represent" attributes of the auth_membership.user_id and auth_membership.group_id fields:

db.auth_membership.user_id.represent = lambda id, row: '%(first_name)s %(last_name)s' % db.auth_user(id)

Note, if you exclude the "id" field from the representation, two users with the same name will appear identically in the list (that's why the default representation includes the "id" -- in order to ensure a unique set of values).

Anthony

Itamar Cohen

unread,
Aug 22, 2015, 12:12:17 PM8/22/15
to web...@googlegroups.com
Thanks Anthony,
It works (both solutions) in the grid but not in the edit/new forms you get when clicking on the auth_memebership link in the grid (the id still appended to the name/role. May be it is OK as you mentioned regarding uniqueness. 

Best Regards
Itamar Cohen
Picatek
Ahornvej 27
DK-7700 Thisted
Denmark
www.picatek.dk
i...@picatek.dk
T: +45 97 91 07 82
M:+45 29 89 07 82

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/rgtfBjm_S38/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
Aug 22, 2015, 2:19:16 PM8/22/15
to web2py-users
Yes, I forgot to mention, the dropdowns in the forms are based in the IS_IN_DB validator attached to each field. So, you will also have to overwrite the "requires" attribute of these two fields with a custom IS_IN_BD validator.

Anthony

Itamar Cohen

unread,
Aug 23, 2015, 9:49:35 AM8/23/15
to web...@googlegroups.com
Thanks for the info.

Best Regards
Itamar Cohen
Picatek
Ahornvej 27
DK-7700 Thisted
Denmark
www.picatek.dk
i...@picatek.dk
T: +45 97 91 07 82
M:+45 29 89 07 82

On 22 August 2015 at 20:19, Anthony <abas...@gmail.com> wrote:
Yes, I forgot to mention, the dropdowns in the forms are based in the IS_IN_DB validator attached to each field. So, you will also have to overwrite the "requires" attribute of these two fields with a custom IS_IN_BD validator.

Anthony

Reply all
Reply to author
Forward
0 new messages