Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
3949 Lượt xem

I'm currently trying to have a field default to a computed value upon creation, but then remain unchanged after the record has been created. I've extended 2 models MrpWorkorder and MrpRouting to add some fields like so:


class MrpRouting(models.Model:
​_name = "mrp.workorder"
​_inherit = "mrp.workorder"

​planning_bucket = fields.Integer("Planning Bucket")

class MrpWorkorder(models.Model):
​_name = "mrp.workorder"
​_inherit = "mrp.workorder"

​planning_bucket = fields.Integer("Planning Bucket",
​default=lambda self: self.operation_id.planning_bucket​
​)

The ideal behavior im going for here, is that when a work order is created, it automatically sets the value of planning_bucket based on the planning_bucket on the associated operation. Then, once the work order is created, the value of planning_bucket should remain unchanged, meaning if i later change the planning_bucket on the operation, it wont change on the work order.

The above code is setting the planning_bucket to 0 on every work order, even if a planning_bucket value exists on the associated operation. I was able to get the values to set properly with the compute argument, but the issue i had with that was that any time the operation's planning bucket changed, the work order planning bucket also changed.


I'm running Odoo 15.0. Any help would be really appreciated!

Ảnh đại diện
Huỷ bỏ
Tác giả Câu trả lời hay nhất

I ended up getting the desired behavior using the create method. I call super() first to create the record and then set the value of planning_bucket after:

@api.model
def create(self, vals):
​res = super(MrpWorkorder, self).create(vals)
​res.planning_bucket = res.operation_id.planning_bucket
​return res


Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 4 22
6382
0
thg 4 23
2985
1
thg 12 21
5008
5
thg 6 21
17909
1
thg 3 21
5780