server-tools icon indicating copy to clipboard operation
server-tools copied to clipboard

auditlog: transient model causes error.

Open obayit opened this issue 8 months ago • 0 comments

If I add an auditlog rule for a model, and that model is inherited by a transient model, an exception will be thrown in the create() method for the transient model.

auditlog

Describe the bug

To Reproduce

Affected versions: 11.0

Steps to reproduce the behavior: Add the following code:

class MyStuff(models.Model):
    _name = 'my.stuff'
    _description = 'My Stuff'

    @api.multi
    def load_edit_wizard(self):
        wizard_obj=self.env['edit.my.stuff']
        # should I just copy all the fields? maybe reuse the copy() of models
        self.ensure_one()
        stuff_data = self.copy_data()[0]  # get values dictionary from the record
        stuff_data['og_record_id'] = self.id
        wizard = wizard_obj.create(stuff_data)

        return {
            'context': self.env.context,
            'name': 'Edit Stuff',
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'edit.my.stuff',
            'res_id': wizard.id,
            'view_id':False,
            'type': 'ir.actions.act_window',
            'target': 'new',
        }


class EditMyStuffWizard(models.TransientModel):
    _name = "edit.my.stuff"
    _inherit = "my.stuff"

    og_record_id = fields.Many2one('my.stuff')

    @api.multi
    def save(self):
        new_values = {
                # here I set values entered by the user form the wizard form
                'some_field': self.some_field,
            }
        self.og_record_id.write(new_values)
  1. Then add a auditlog rule for the model 'my.stuff'.
  2. Call the method load_edit_wizard from any record, A KeyError exception will be thrown, from this line in the auditlog code:
                'model_id': self.pool._auditlog_model_cache[res_model],

The full stacktrace (I tested with a click-odoo script):

Traceback (most recent call last):
  File "/home/username/.virtualenvs/odoo11-37/lib/python3.7/site-packages/click_odoo/env_options.py", line 195, in _invoke
    return self.org_invoke(ctx)
  File "/home/username/.virtualenvs/odoo11-37/lib/python3.7/site-packages/click/core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/home/username/.virtualenvs/odoo11-37/lib/python3.7/site-packages/click/core.py", line 783, in invoke
    return __callback(*args, **kwargs)
  File "/home/username/.virtualenvs/odoo11-37/lib/python3.7/site-packages/click_odoo/cli.py", line 45, in main
    script, init_globals=global_vars, run_name="__main__"
  File "/usr/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/usr/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/username/src/loc/my_module/models/sketch.py", line 35, in <module>
    res = stuff.load_edit_wizard()
  File "/home/username/src/loc/my_module/models/my_stuff.py", line 4610, in load_edit_wizard
    wizard = wizard_obj.create(stuff_data)
  File "/home/username/src/loc/oca_community11/auditlog/models/rule.py", line 246, in create_fast
    'create', None, new_values, {'log_type': log_type})
  File "/home/username/src/loc/oca_community11/auditlog/models/rule.py", line 372, in create_logs
    'model_id': self.pool._auditlog_model_cache[res_model],
KeyError: 'edit.my.stuff'
Error: 'edit.my.stuff'

Expected behavior

Additional context The main cause is probably that I inherited a Model in a TransientModel

obayit avatar Apr 30 '25 14:04 obayit