Skip to Content
Menu
This question has been flagged
5 Replies
5017 Views

Hii

see my code below.

add_field.py

class CrmLead(models.Model):
    _inherit = 'crm.lead'

    add_portal_id = fields.Many2one('add.portal')

class AddPortal(models.Model):
    _name = "add.portal"
    _description = "Add Portal"
    _rec_name = "add_portal_name"

    add_portal_name = fields.Char(string='Add Portal Name', required=True)

add_field_view.xml

<record id="profile_field_form_inherit" model="ir.ui.view">
    <field name="name">Add Profie Field</field>
    <field name="model">crm.lead</field>
    <field name="inherit_id" ref="crm.crm_case_form_view_oppor"/>
    <field name="arch" type="xml">
        <field name="team_id" position="after">
            <field name="add_portal_id" string="Portal Name"/>
        </field>
    </field>
</record>

Avatar
Discard

The need of this post is, to sometime we need to visible invisible, readonly etc some filed on the basis of login user group or we want to perform some action on the basis of login user. To do so we need to get login user group: https://goo.gl/Ts3qqK

How to visible and invisible fields in odoo: https://goo.gl/BCxCpk

Best Answer

Hi  Kiran,

You can achieve this by using following code

In Python

from lxml import etree
@api.model
def fields_view_get(self, view_id='you_form_view_id', view_type='form', toolbar=False, submenu=False):
res = super(ClassName, self).fields_view_get(view_id=view_id, view_type=view_type, toolbar=toolbar,
submenu=submenu)
doc = etree.XML(res['arch'])
if self.env.user.has_group('group1'):
for node in doc.xpath("//field[@name='field1']"):
node.set('modifiers', '{"readonly": true}')
for node in doc.xpath("//field[@name='field2']"):
node.set('modifiers', '{"readonly": true}')
elif self.env.user.has_group('group2'):
for node in doc.xpath("//field[@name='field2']"):
node.set('modifiers', '{"readonly": true}')
for node in doc.xpath("//field[@name='field3']"):
node.set('modifiers', '{"readonly": true}')
res['arch'] = etree.tostring(doc)
return res

You need to assign that user to specific Group based on that group you can achieve that easily

Othewise based on Login also, you can achieve

Thanks

Avatar
Discard
Author

Hi @Gokulakrishnan

i used this above code but error are occurred you can see below error

NameError: name 'TrailQueue' is not defined

how to define TrailQueue tell me

Thanks In advanced

Hi KIran,

That's Class name of the Method.

Best Answer

Hi if you want to make the field readonly for a particular group, say for all the users present in a group.. you can use fields view get function to make the field readonly for a particular user.. or you can create your own record rules... 

There is another alternative, you can create a boolean field and make it invisible.. use compute function to make the field True/False based on the user logged in using self.id .... you can then use this boolean field in attrs for your field...

<field name="add_portal_id" attrs="{'readonly': [('your_boolean_field', '=', True)]}"/>

Avatar
Discard
Best Answer

Here attrs will not work for the dynamic domain so (maybe only) option is passed context from the action and eval it on the field

like pass 'readonly_user' key from action context base on a particular user and

            <field name="add_portal_id"  string="Portal Name" readonly="context.get('readonly_user', False)"/>

Avatar
Discard
Author

Hello @Ravi you told that pass the 'readonly_user' key means their will be put the user's username or what plz tell me clearly.

thanks

Related Posts Replies Views Activity
1
Apr 19
8803
1
Feb 19
4614
1
Mar 22
10316
1
Sep 21
4724
3
Jan 20
5849