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.

4

Is it possible to make a field readonly for users under particular group in openerp 7?

By
Omal Bastin
on 3/22/13, 10:17 AM 21,540 views

Hi, I have a field in invoice(say reference number). Only the users coming under the group 'Financial Manager' can edit the field. All other users can view these fields as readonly field. Is it possible to achieve this?

Thanks in advance.

@Omal Bastin  Thanks for your answer . It really helped me in my project.

Surendar
on 4/6/15, 9:03 AM
10

Omal Bastin

--Omal Bastin--
1146
| 6 6 8
Kochi, Kerala, India
--Omal Bastin--

Swiming deep into openERP/ODOO

Omal Bastin
On 11/28/13, 9:01 AM

Another simple solution.

First create your form view. Then inherit the view also specify the groups. for example in sale order form view, i want to make the customer reference(client_order_ref) field is readonly for everyone except group group_financial.

<record id="view_order_form_cust_ref_readonly" model="ir.ui.view">
    <field name="name">sale.order.form.readonly.cust</field>
    <field name="model">sale.order</field>
    <field name="inherit_id" ref="sale.view_order_form"/>
    <field name="groups_id" eval="[(6, 0, [ref('base.group_financial') ])]"/>
    <field name="arch" type="xml">
        <field name='client_order_ref' position="attributes">
            <attribute name="readonly">0</attribute>
        </field>
    </field>
</record>

This is a view customization which can be easily bypassed. It should be used only for useability, not access control and security.

Mohammad Alhashash
on 11/29/13, 6:50 AM

Using direct json call for example.

Mohammad Alhashash
on 11/29/13, 9:13 AM

Okey then how can we make a field readonly based on group and status/state?

Omal Bastin
on 11/29/13, 9:19 AM

Try reading my answer to this question.

Mohammad Alhashash
on 11/29/13, 10:50 AM

function field will only execute when saving the record. i dont think create/write is a good option and also the property field.

Omal Bastin
on 11/29/13, 11:02 AM

what if the whole view needs to readonly? Is that possible??

Alcaline
on 12/16/14, 10:33 PM
4

Mohammad Alhashash

--Mohammad Alhashash--
2075
| 5 5 8
Giza, Egypt
--Mohammad Alhashash--

Developer

Mohammad Alhashash
On 3/22/13, 1:16 PM

If you need to enforce access control to the field, you should not use view customization as it is just a cosmetic behavior and can be easily bypassed.

For a real field/column access control, you should use one of the following options:

Option 1: Use the new field level access control

Using groups='group1, group2, ...' in field definition restricts the field to the specified groups. Currently, you cannot control specific read or write, only allow or prevent access.

In this case, you may create functional field that can be displayed for restricted users. The function for that fields must use the user id openerp.SUPERUSER_ID or integer 1 to read the original field, not the current user id.

from openerp import SUPERUSER_ID
....
class MyClass(osv.Model):
...
    def _myfield_reader(self, cr, uid, ids, name, args, context=None):
        values = self.read(cr, uid, SUPERUSER_ID, ids, ['myfield'], context=context)
        return {v['id']:v['myfield'] for v in values}

Option 2: Override create() and write() methods

By overriding these methods, you can raise AccessError exception if a restricted user tried to write that field value.


Option 3: Use fields.property field

Property fields are stored as ir.property records. So, standard record access control can be used to protect it. Check for example property_account_* fields defined for res.partner in account/partner.py in the account module.

i want to make the invoice sequence fields in account.config.settings view in openerp 7(in Settings/Configuration/Accounting) to be readonly for every users except admin(uid=1)

Omal Bastin
on 3/25/13, 2:19 AM

You should use a record rule directly on the sequence object. The configuration wizard just takes the user input and applies the change to the sequence object. It is a different concept; check this answer. You can ask another question and someone may help.

Mohammad Alhashash
on 3/25/13, 4:48 AM

i didnt get a correct answer but i hope these are some options that we can do. so accepting it as answer

Omal Bastin
on 3/27/13, 3:50 PM

Suppose I have many2one field "demo_field" and I have two groups "demogroup1","demogroup2" then for demo1 group I want demo_field as default functoinality of many2one field and for demogroup2 demo_field is readonly then what can I do?

Vaibhav
on 7/12/13, 5:18 AM

@vaibhav please check the answer that is marked as correct answer

Omal Bastin
on 12/2/13, 5:43 AM

Thanks @Mohammad: Nice and meaning-full answer :)

dirtyHandsPHP
on 10/30/14, 6:29 AM
0

Yannick Vaucher

--Yannick Vaucher--
1699
| 5 7 9
Lausanne, Switzerland
--Yannick Vaucher--
Yannick Vaucher
On 3/22/13, 10:30 AM

If you need it widely, what you can do is override the fields_view_get method.

If this is only for few fields, you can create a readonly related field and use groups in xml to define which one is seen.

<field name="my_field" groups="group_editors"/>
<field name="rel_my_field" groups="group_viewers"/>

View customization using fields_view_get or groups=... XML attributes is just for usability, not a security measure. Fields can still be updated easily using simple json queries in browser console.

Mohammad Alhashash
on 3/22/13, 11:35 AM

Thanks Mohammad, As question was not tagged with security tag previously, I understood it only as a matter of readonly in visualization. It depends if this is a real security issue or simply a way to ease user's experience.

Camptocamp SA
on 3/25/13, 2:00 PM

Suppose I have many2one field "demo_field" and I have two groups "demogroup1","demogroup2" then for demo1 group I want demo_field as default functionality of many2one field and for demogroup2 demo_field is readonly then what can I do?

Vaibhav
on 7/12/13, 5:18 AM

@vaibhav please check the answer that is marked as correct answer

Omal Bastin
on 12/2/13, 5:42 AM

but related field is not storing in DB ? So how to display that field in tree view ?

Vaibhav
on 12/3/13, 1:38 AM
0

Borni DHIFI

--Borni DHIFI--
5102
| 6 8 9
Tunis, Tunisia
--Borni DHIFI--

Techno Functional Consultant Odoo(OpenERP) 

http://www.linkedin.com/in/borni

Skype : dhifi.borni

Borni DHIFI
On 3/22/13, 10:48 AM

Basic field properties (Required,Readonly,Searchable) can not be modified by the configuration! must change the code in Python and preferably by adding a new module. but you can edit fields personalized.

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

3 follower(s)

Stats

Asked: 3/22/13, 10:17 AM
Seen: 21540 times
Last updated: 5/7/16, 11:20 AM