Skip to Content
Menu
This question has been flagged
1 Reply
1623 Views

Hi,

In Odoo 11, in the contact (res.partner) table, I added a "locked" field (boolean), and buttons to lock or unlock the data. These buttons are visible only when the contact form is edited (with the "oe_edit_only" class). In unlocked state (self.locked == False), a specific field appears and can be modified.

This is working well.

When I save the contact (button "Save"), I would like to automatically change the locked field value to True.

Thanks in advance,

Boris

Avatar
Discard
Author Best Answer

I'm using a cron to setup all the "locked" to True, every day, but it's not what I want.

Boris

Avatar
Discard
Author

I find a solution with o lock/unlock button:

from odoo import models, fields, api

class ResPartner(models.Model):
_inherit = 'res.partner'
locked = fields.Boolean(string='Locked', default=False) # I want unlocked at creation

def set_locked(self):
self = self.with_context(set_locked=True)
self.locked = not self.locked

@api.model
def create(self, values):
values['locked'] = True
return super(resPartner, self).create(values)

@api.multi
def write(self, values):
if not self.env.context.get('set_locked'):
values['locked'] = True
super(ResPartner, self).write(vales)

rec = super(ResPartner, self).write(values)
self = self.with_context(set_locked=False)
return rec

In the res.partner form view:
<field name="arch" type="xml">

<xpath expr="//sheet" position="before">
<header>
<field name="locked" invisible="True"/>
<button string="Lock" name="set_locked" type="object" attrs="{'invisible': [('locked', '=', True)]}"/>
<button string="Unlock" name="set_locked" type="object" attrs="{'invisible': [('locked', '=', False)]}"/>
</header>
</xpath>

<field name="my_field" position="attributes">
<attribute name="readonly">[('locked', '=', True)]</attribute>
</field>

<field>