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.

1

Set default value for a many2one field

By
aci aisha
on 2/4/15, 11:24 PM 4,201 views

Friends,

I have a filed file_type(Its a many2one field)

I have to set default value for it for different users..

Example,

I have 3 user trademark_user,company_user & all user...

For trademark user the file_type field will be "Trademark Registeration" and it should be read only..

For company user the file_type field will be "Company Registeration" and it should be read only..

For all user the file_type field will visible all and it should not be read only..

Thanks in advance..

6

Vasanth

--Vasanth--
3458
| 5 3 8
Pondicherry, India
--Vasanth--
Vasanth
On 2/4/15, 11:51 PM

Hi,You try this:

<field name="file_type" position="attributes">

<attribute name="attrs">{'readonly': ['|',('file_type',in',['trademark_user','company_user']]}</attribute>

then follow the instructions:

1.Log in into any one of the user(trademark_user,or company_user or all user)

2.Enable the developer mode in openerp

3.choose any one of the type in the field "file_type"

4.Then in the debug view menu,there is a menu called " Set Default"

5.you must select your field in the "Default:" field by using the drop-down list

6.Below the "Default:" field, You can see two options called "only you" and "All users".if you select "only you" option the field value used for the customer you logged in.if "All users" means it should be used for all the users.

7.save the value. 8.Log out the current user and log in the next user and continue the above process.

 

whenever you sets a default value in this method,it stores in the Settings/Technical.Actions/User-defined defaults with the field name and model name.you can delete it there .

 

hope it is useful for you

 

k,,lt mi try vasanth

aci aisha
on 2/4/15, 11:54 PM

I think this code make the field readonly for these 2 users,,,but how i set default values for this users

aci aisha
on 2/4/15, 11:55 PM

ok.then follow the instructions: 1.Log in into any one of the user(trademark_user,or company_user or all user) 2.Enable the developer mode in openerp 3.choose any one of the type in the field "file_type" 4.Then in the debug view menu,there is a menu called " Set Default" 5.you must select your field in the "Default:" field by using the drop-down list 6.Below the "Default:" field, You can see two options called "only you" and "All users".if you select "only you" option the field value used for the customer you logged in.if "All users" means it should be used for all the users. 7.save the value. 8.Log out the current user and log in the next user and continue the above process. Hope this will usefull for you.

Vasanth
on 2/5/15, 12:12 AM

Ok Vasanth,It works Fine.. But I cant edit if i set ones.And also is this change will gone if we upgrade any module??

aci aisha
on 2/5/15, 12:41 AM

you can edit.just change the value you want to change in the field and make it as default.i wont be change when the module upgrade.

Vasanth
on 2/5/15, 12:46 AM

I can change the default value.But i didn't remove it(No default valu) Hw it is possible??

aci aisha
on 2/5/15, 12:53 AM

whenever you sets a default value in this method,it stores in the Settings/Technical.Actions/User-defined defaults with the field name and model name.you can delete it there .

Vasanth
on 2/5/15, 1:31 AM

Ok,,Nic..Thankss... Can i give u 1 vote & set it as correct(1 tick)

aci aisha
on 2/5/15, 1:40 AM

ok...

Vasanth
on 2/5/15, 1:41 AM

But 1 problem Vasanth...?!!

aci aisha
on 2/5/15, 1:42 AM

What problem?

Vasanth
on 2/5/15, 1:45 AM

The answer not helps me,But ur comments helped.. How can i give point for comments??

aci aisha
on 2/5/15, 1:53 AM

i converted the comments as answer

Vasanth
on 2/5/15, 2:03 AM

Yup,,I giv u 1 tick & vote

aci aisha
on 2/5/15, 2:11 AM
0
kirubanidhi
On 7/1/16, 3:10 AM

try to appliy this way,

ur_field = fields.Many2one('Your model', string="Your specified name",default=lambda self: self.env['Your model'].search([('file_type','=','company_user')]))

0

Atchuthan - Technical Consultant, Sodexis Inc

--Atchuthan - Technical Consultant, Sodexis Inc--
3883
| 5 3 8
Puducherry, India
--Atchuthan - Technical Consultant, Sodexis Inc--
Passionate coder in Python/Odoo(formerly known as OpenERP).

Contact me via GMail: atchuthantu@gmail.com via Skype: atchuthan_skype
via Linkedin: http://in.linkedin.com/in/atchuthantu
Atchuthan - Technical Consultant, Sodexis Inc
On 2/5/15, 1:59 AM

Create 2 groups at your security file and set these groups to their corresponding users.
       <record id="group_trademark" model="res.groups">
            <field name="name">Trademark user</field>
        </record>
       <record id="group_company" model="res.groups">
            <field name="name">Company user</field>
        </record>

In py,
    def _get_file_type(self,cr,uid,context):
        file_type = False
        if self.pool.get('res.users').has_group(cr,uid , 'YOUR MODULE.group_trademark'):
            file_type = 'trademark'     # use the key of your selection field to select "trademark" @ file_type
        if self.pool.get('res.users').has_group(cr,uid , 'YOUR MODULE.group_company'):
            file_type = 'company'   
        return file_type
    
    _defaults={
        'file_type' : _get_file_type
    }

In View.XML, inherit your view to make the field readonly for these 2 groups.
        <record model="ir.ui.view" id="object_name_form_view">
            <field name="name">your_objectname.form</field>
            <field name="model">your_objectname</field>
            <field name="type">form</field>
            <field name="inherit_id" ref="your_view_name" />
            <field name="groups_id" eval="[(6,0,[ref('YOUR MODULE.group_trademark'),ref('YOUR MODULE.group_company')])]" />
            <field name="arch" type="xml">
                <field name="file_type" position="attributes">
                    <attribute name="readonly">True</attribute>
                </field>
            </field>
        </record>
            

Atchthan,Its didnt work..Can we change 'file_type' : _get_file_type as 'file_type' : _get_file_type ()

aci aisha
on 2/5/15, 2:10 AM

what error did you get?

Atchuthan - Technical Consultant, Sodexis Inc
on 2/9/15, 12:25 AM

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

1 follower(s)

Stats

Asked: 2/4/15, 11:24 PM
Seen: 4201 times
Last updated: 7/1/16, 3:10 AM