how to get the user's group as a default value

119 views
Skip to first unread message

Ryan

unread,
Oct 5, 2016, 3:30:08 AM10/5/16
to tryton

1) The Idea is to get the user's group and use it as a default value, 2) and later to be filtered with a domain on the next Many2One field to show only documents (user created table) assign to the group.

1) I do not know how to get the group name of the user log in (so need to pick from the groupname)
2) working correctly but user needs to chose first their group via groupname

Thanks for any help.

------------

 groupname = fields.Many2One('res.group', 'Group', required=False)
 group = fields.Char('Group')
 code = fields.Many2One('nhp.documents', 'Document', domain=[('group','=',Eval('groupname'))])

 @staticmethod
    def default_group():
        return 'NHP'

------------

Cédric Krier

unread,
Oct 5, 2016, 4:15:04 AM10/5/16
to tryton
On 2016-10-04 23:57, Ryan wrote:
>
> 1) The Idea is to get the user's group and use it as a default value, 2)
> and later to be filtered with a domain on the next Many2One field to show
> only documents (user created table) assign to the group.
>
> 1) I do not know how to get the group name of the user log in (so need to
> pick from the groupname)

You have access to the user id here:
http://doc.tryton.org/4.0/trytond/doc/ref/transaction.html#trytond.transaction.Transaction.user

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Ryan

unread,
Oct 8, 2016, 6:15:13 AM10/8/16
to tryton
Thanks Sir Cedric,

Just a newbie here, just a month ago... 

Can you please show me the code for creating a query to check the user's id to get his group.
does the user group can be found on 'res_group'? or res_user-res_group?

Thanks again for any help. 
Best Regards... 
//elite

Sergi Almacellas Abellana

unread,
Oct 8, 2016, 9:43:20 AM10/8/16
to try...@googlegroups.com
El 08/10/16 a les 12:07, Ryan ha escrit:
> Thanks Sir Cedric,
>
> Just a newbie here, just a month ago...
>
> Can you please show me the code for creating a query to check the user's
> id to get his group.
> does the user group can be found on 'res_group'? or res_user-res_group?
You have to browse the user with the id from the context of transaction
user and then read it's group properties. Something like (sorry but
writting from memory):

pool = Pool()
User = pool.get('res.user')
user = User(Transaction().context['user']
user.groups # This is a tuple of res.group instances for each group of
the user.

Hope it helps.

--
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

Ryan

unread,
Oct 9, 2016, 3:00:04 AM10/9/16
to tryton
Thanks Sir Sergi. 

I'll try it later and update this trend asap.

Best Regards...
//elite

Ryan

unread,
Oct 11, 2016, 3:15:05 AM10/11/16
to tryton


On Sunday, October 9, 2016 at 3:00:04 PM UTC+8, Ryan wrote:
Thanks Sir Sergi. 

I'll try it later and update this trend asap.

sorry... but I got blank result on this: please see code below. any help please...
    
z_group = fields.Char('Default Group')


@staticmethod
def default_zgroup():
      pool = Pool() 
      User = pool.get('res.user') 
      user = User(Transaction().context['user']) 
      return user.groups 

Cédric Krier

unread,
Oct 11, 2016, 3:35:04 AM10/11/16
to tryton
On 2016-10-10 18:28, Ryan wrote:
>
>
> On Sunday, October 9, 2016 at 3:00:04 PM UTC+8, Ryan wrote:
> >
> > Thanks Sir Sergi.
> >
> > I'll try it later and update this trend asap.
> >
> > sorry... but I got blank result on this: please see code below. any help
> please...
>
> z_group = fields.Char('Default Group')
>
>
> @staticmethod
> def default_zgroup():
> pool = Pool()
> User = pool.get('res.user')
> user = User(Transaction().context['user'])
> return user.groups

You can not return instances on default method because this must be sent
to the client over wire.
Also the type of your fields are not compatible, you can not store a
list on a Char field.


PS: Please do not top-post on this mailing list, see
http://groups.tryton.org/netiquette

Sergi Almacellas Abellana

unread,
Oct 11, 2016, 3:35:46 AM10/11/16
to try...@googlegroups.com
El 11/10/16 a les 03:28, Ryan ha escrit:
>
>
> On Sunday, October 9, 2016 at 3:00:04 PM UTC+8, Ryan wrote:
>
> Thanks Sir Sergi.
>
> I'll try it later and update this trend asap.
>
> sorry... but I got blank result on this: please see code below. any help
> please...
>
> z_group = fields.Char('Default Group')
>
>
> @staticmethod
> def default_zgroup():
> pool = Pool()
> User = pool.get('res.user')
> user = User(Transaction().context['user'])
> return user.groups
Here are returning user.group instances instead of string, which is the
expected behaviour of the Char fields. If you wan to return a string,
you can return the rec_names of the groups joined by comas using:

return ','.join([u.rec_name for r in user.groups])
Reply all
Reply to author
Forward
0 new messages