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

default_user_get function

By
daouda
on 3/29/13, 7:07 AM 4,107 views

Hi, I'am developping a module where i have a field named "user_id" in this field i would like to fill lit automatically by the name of the current user, exactly like it's done in crm.lead module with the field vendor. I know a function named default_user_get is defined but i don't know in what module?. Thanks for your answers

waiting for someone else answer

daouda
on 3/29/13, 11:03 AM
0
Saloua ELKHOUFAIRI
On 4/21/15, 9:37 PM

Thank you Mike a lot your solution helps me a lot :)

0
Mike Telahun
On 3/30/13, 1:22 PM

To use the currently logged in user's id as a default value you don't need the specific function in crm/crm_lead.py. You can use a simple lambda function:

_defaults = {
    'my_field': lambda s, cr, uid, c: uid,
}

If you need to do any special processing (like in crm/crm_lead.py) you can use a function:

def _get_my_field(self, cr, uid, context=None):
    if ...:
        ....
        return uid
    else:
        ...
        return False

_defaults = {
    'my_field': _get_my_field,
}

If you really need to use the base_stage mixin, then you have to import it into you module and inherit from it in your class:

from openerp.addons.base_status.base_stage import base_stage

class my_class(base_stage, osv.Model):
    ...
    ...
    _defaults = {
        'my_field': lambda s, cr, uid, c: s._get_default_user(cr, uid, c),
    }
0

patrick

--patrick--
1958
| 5 4 7
Baarn, Netherlands
--patrick--
patrick
On 3/29/13, 7:16 AM

Check the file addons\crm\crm_lead_view.xml. There you find the screen definition for the LEADS screen.

The field which is filled with the user logged in, is called "user_id". It looks like it is automatically filled with the content / name of the user with ID = uid (uid is a variable which is always needed in every call to a method within openERP. It is the User ID, hence uid). In order to fill it, you might have to copy uid from the call of the method/function to that field.

<update> In the file addons/base_status/base_state.py there is a method _get_default_user defined

#def _get_default_user(self, cr, uid, context=None):

""" Gives current user id

   :param context: if portal not in context returns False

"""
if context is None:
    context = {}
if not context or not context.get('portal'):
    return False
return uid

So as I said before, it is just using the given uid.

Conclusion: If you need to store the user, use uid from the input.

Ok I see it. But what i really want is in addons/crm/crm_lead.py. If you look the defaults definition you'ill see " 'user_id': lambda s, cr, uid, c: s._get_default_user(cr, uid, c), it's the _get_default_user that i wanna see it's definition

daouda
on 3/29/13, 9:32 AM

yes i See it but i get the whole list of ids of all the users and i want only the current user id

daouda
on 3/29/13, 6:58 PM

Douada, you mean if you the following, you get all the users? my_field = uid

patrick
on 3/29/13, 7:58 PM

i didn't do my_field = uid. I did my_field = lambda s, cr, uid, c: s._get_default_user(cr, uid, c),, because i copied the _get_default_user function

daouda
on 3/29/13, 8:07 PM

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

0 follower(s)

Stats

Asked: 3/29/13, 7:07 AM
Seen: 4107 times
Last updated: 4/21/15, 9:37 PM