Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
1 ตอบกลับ
469 มุมมอง

Hello,

I'm working on a custom model mymodel that logs user actions in a related model mymodel.history. The user_id is set using self.env.uid (same rec.env.user.id). The issue is not the saved value the correct user ID is stored in the database but in the UI (form or tree view), the user is always shown as OdooBot.


def _create_history_record(self, old_state, new_state):

    for rec in self:

        self.env["mymodel.history"].create({

            "user_id": self.env.uid,

            "mymodel_id": rec.id,

            "old_state": old_state,

            "new_state": new_state,

        })


อวตาร
ละทิ้ง

What's your view then?

คำตอบที่ดีที่สุด

Hi,

Ensure that:

  1. user_id in mymodel.history is defined as a Many2one field linking to res.users .
  2. The field is displayed in views (form/tree) with the correct relation and context.


Model Definition:

from odoo import models, fields class MyModelHistory (models.Model): _name = 'mymodel.history' _description = 'My Model History' user_id = fields.Many2one( 'res.users' , string = 'User' , default= lambda self: self.env.uid ) mymodel_id = fields.Many2one( 'mymodel' , string= 'My Model' ) old_state = fields.Char( 'Old State' ) new_state = fields.Char( 'New State' )

View Definition Example (XML):

<record id = "view_mymodel_history_tree" model = "ir.ui.view" > <field name = "name" >mymodel.history.tree </field> <field name = "model" >mymodel.history </field> <field name = "arch" type = "xml"> <tree> <field name = "user_id"/> <field name = "mymodel_id"/> <field name = "old_state"/> <field name = "new_state"/> </tree> </field> </record>

Creating Records in Code:

What you have is fine, but for clarity, you can use:

def _create_history_record ( self, old_state, new_state ): for rec in self: self.env[ "mymodel.history" ].create({ "user_id" : self.env.uid, "mymodel_id" : rec. id , "old_state" : old_state, "new_state" : new_state, })

or even better (Odoo best practice):

def _create_history_record ( self, old_state, new_state ): for rec in self: self.env[ "mymodel.history" ].sudo().create({ "user_id" : self.env.user. id , "mymodel_id" : rec. id , "old_state" : old_state, "new_state" : new_state, })

i hope it is usefull

อวตาร
ละทิ้ง
ผู้เขียน

Great! The problem was that I had related my model to res.partner, which wasn't very logical.
Thank you !

Related Posts ตอบกลับ มุมมอง กิจกรรม
3
มิ.ย. 25
2401
Change resume view แก้ไขแล้ว
4
ก.ค. 25
1148
1
พ.ย. 24
1557
0
ต.ค. 23
4488
0
ก.ค. 22
2871