Shall I record the user name who modify a certain field by Odoo?

570 views Asked by At

.py file: ….

namex=fields.Text()

moifier=fields.Many2one(‘res.users’, string=”Modifier”)

When some user modify “namex”, his/her name should be recorded on field “modifier” automatically; what code should I make? I try “onchange/depends”, but failed; maybe modifier could be a “text field/ char field”?

in addition, shall I set "access_rule" to set users just see the records created by the members in his/her own group?

2

There are 2 answers

1
Hung Hoang On BEST ANSWER

Odoo already has that for you. Every model has those fields, which are automatically created and updated each time you create, or write:

  • create_date (datetime): when record is created
  • create_uid (many2one): user who created this record
  • write_date (datetime): last time record is updated
  • write_uid (many2one): last user updated this record

Go to Settings > Technical > Database Structure > Models for more details.

0
Andrei Boyanov On

While Odoo will keep for you a track of the last user which has modified a record, a modifier per field is not kept. I can see the interest of such a functionality in many cases.

To do that for a particular model one possibility is to redefine the write method of this model. In your .py file you may want to add something like this:

@api.model
def write(self):
    if self.namex in values:
        values.update({'modifier': uid})
    super().write(cr, uid, ids, values, context)

Another way to do that in a more flexible way is to use the @onchange decorator:

@onchange('your_sensible_field_name'):
def set_modifier(self):
    self.modifer = self.env.user

You may also want to take a look at the @depends decorator.