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

Hello. Before start some code:

class ExampleModelItem(models.Model):
_name = "example.model.item"
partner_id = fields.Many2one(comodel_name='res.partner')
lines = fields.Many2many(comodel_name='sale.order.line')

@api.onchange("partner_id")
def _compute_sale_lines(self):
ctx_obj = self._context['reglas']

class ExampleModel(models.Model):
_name = "example.model"
reglas = fields.One2many(comodel_name='example.model.item', inverse_name='...', string="...")
lines = fields.Many2many(comodel_name='sale.order.line')

The view in "example.model" looks like :


<field name="reglas" context="{'reglas': reglas}">
<tree>...</tree>
<form>...</form>
</field>

As you can see in the view we set the field in the context, we get the value to ctx_obj variable in the _compute function.

This ctx_obj have a list of the old o2m structure [(0, False, {...}), (1, 1, {...}), (4, 3, False), ..., (type, id, data)]

The problem i facing is, how can i know in the compute method if the record is one of the records in the ctx_obj? Or even better, wich one. When self.id exists its easy, because i can find that id in the ctx_obj. But when self.id is a Newid object, the ctx_obj will be a False. Is there a way to acknowledge is the newid is already in the context object or not??


Thank you in advance

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

The problem is not o get values to the function. As you can see the data reach the function.

Let me elaborate it a little more:

Beeing in the main form, i have values {'reglas': [], 'lines': [(6, 0, [100201, 100202])]}

No i create a new subform, and when the function is called i have ctx_obj=[]. The function runs, data is build and back to the main form with values {'reglas': [(0, False, {'partner_id': 38, 'lines': [(6, 0, [100201])]})]}.

Now if i edit again the same item before i save the main form, and change partner_id....ctx_obj=[(0, False, {'partner_id': 38, 'lines': [(6, 0, [100201])]})], and the record doesnt have an ID.

If instead of edit, i create a second one, ctx_obj is the same. The main point of this post is, how can i relate the record to the item inside ctx_obj??

For now a strinct rule of "not edit not saved data" work, but i would like to remove the problem before impose such thing.

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

Context must be specified on the source-field of the onchange.

In your case, ensure whether you are passing the necessary context on the field 'partner_id'

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
1
thg 3 15
5396
3
thg 3 15
4425
1
thg 10 23
2027
2
thg 10 23
2256
2
thg 8 23
4268