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

Hi, I'm using Odoo 11, I would like to know if I'm doing the creation and manipulation of records on cache correctly.

This is a simplified version of my issue:

class model_A(model.Model):
    model_A_some_field = fields.Char()
    model_B_ids = fields.One2many('model_B', 'model_A_id')
class model_B(model.Model):
    model_A_id = fields.Many2one('model_A')
    model_B_some_field = fields.Char()

I need to create, during edition of the form of model_A, a model_B record. I achieved this using an onchange method and 'new' on the model_A class:

@api.onchange('model_A_some_field')
def _onchange_create_record(self):
    self.env['model_B'].new({
        model_A_id = self.id,
        model_B_some_field = 'text'
    })

The problem I'm having is that the model_B record gets created but the fields it has don't appear initialized. I randomly discovered that they get initialized only if they are present as columns on the list view of model_B_ids of the model_A form. Is this the only way to make the cache aware of the records created with new or am I missing something?

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

class model_A(model.Model):

@api.onchange('model_A_some_field')

def _onchange_create_record(self):
    self.env['model_B'].create({
        model_A_id = self.id,
        model_B_some_field = 'text'
    })

Try it


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

Sorry, Kevin, I don't have enough karma to comment on your answer so I'm writing here, I hope you get a notification.

The problem of doing a 'create' during edition is that, if you discard the changes, the created records would stay because they are persisted in the database. By using 'new' they are only stored in cache and saved when the form is saved and discarded otherwise.

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 7 22
9202
1
thg 6 21
2484
2
thg 2 21
299
5
thg 9 20
14367
4
thg 6 20
49758