Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

how to retrieve user first and last name?

By
Andrew
on 7/15/14, 9:55 AM 1,825 views

Hello, i'm writing a module for OpenErp and i have some troubles with user's first and last names.

 

this is my piece of code:

def _user_get(self, cr, uid, context=None):
    cr.execute("select id,name from resource_resource")
    return cr.fetchall()
 .....

 

_columns = {

....

'users' : fields.selection(_user_get, 'Users')

...}

 

But it doesn't work. Can someone help me?

It saves data in the table, but does't show user name and family. The rest info is displayed.

Thank you  * Bole [1]  *for your answer. I know about "'my_user':fields.many2one('res.users','User')", but the problem is that it displays too much info, such as "Language, Login and date". But i need only first and last name [1] https://www.odoo.com/forum/help-1/user/6104

Andrew
on 7/15/14, 10:24 AM

o, so create a new view that shows only the fields you need .. in xml do like: that should do the trick of custom tree view fro many 2 one;) also you can define custom form view;) the same way

Bole
on 7/16/14, 3:23 AM

i have edited my answer so now you have example on how to show only the fields you want , and not all of them ;)

Bole
on 7/16/14, 3:26 AM
1

Bole

--Bole--
2385
| 5 4 6
Zagreb | Karlovac, Croatia
--Bole--

there is only 10 kind of people the ones that understand binary ... and others

Bole
On 7/15/14, 10:03 AM

Well.. you are looking at wrong data.. 

In odoo/openerp resource_resource contains(actualy is hr_employee name i related to this field) 
 the name for employees, not users.. 

there is a table res_users where some user data is stored... (login, password, mail.. etc..) and a relation to res_partner (partner_id) wich contains actual name of user, along with other data (address, phone , mail....)

So eather you rewrite your sql acordingly (using some joins for foreign key ids..) ,

or.. simply user orm methods like.. if you want to search for user wich has login : 'someuser' it would look like:

ids = self.pool.get('res.users').search(cr, uid, [('login'.'=','someuser')])    #expecting a list of ids returned

now that you have list of ids, you can browse those records and read/modify any data stored in fields...

 

but in your case... i think it would be much easier if you do not use fields.selection.. but instead, 

define your:

_columns = {
...
'my_user':fields.many2one('res.users','User'),
...
}
 

this is common usage for many to one field, and it preserves most orm methods in it ( addins and modifying user) 
in case you want to show just a selection on the view... define a view like: (addon after comment:)
<field name="my_user" widget="selection" /> 

If you need only a few fields shown and not the default view for related field, define them in the same view like:

<field name="my_user" widget="selection" > 
    <tree>
        <field name="first_name"/>
        <filed name="last_name"/>
    </tree>
</field>


 

0
Andrew
On 7/17/14, 3:03 AM

Thank you. I solved this isue in the next way:

_columns = {

...

'user_id': fields.many2many('res.users'),

...

}

 

but in the xml view I inserted this string

 

<field name="user_id" string='Assigned Users' widget="many2many_tags" />

0
Zahin
On 7/16/14, 12:24 AM

HI. Andrew 

Here right way TODO.

_columns = {

          'user_id': fields.many2one('res.users', 'Salesperson',),

}

_defaults = {
         'user_id': lambda obj, cr, uid, context: uid,

}

I suggest you to read existing code of sale module or account module, and just go through code and understand existing codes and utilize it in your modules.

 

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

2 follower(s)

Stats

Asked: 7/15/14, 9:55 AM
Seen: 1825 times
Last updated: 3/16/15, 8:10 AM