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

My code: Code below makes no difference in field.

backend_id = fields.Many2one(comodel_name='qb.backend',
                                      string='Quick Backend', store=True,
                                      readonly=False, required=False, default=lambda self:
                                      self.env['qb.backend'].search([]).id
                                      )

I want to set default value of the field in existing records. As I am able to set that field with default value at the time of creating new record with the help of default_get function, but existing records need to be filled manually or by selecting the value.

Example:

_name='qb.backend'

name = fields.char()

 When this field is saved, I want the value of this field to be assigned to the many2one related field in other models.

Backend field exist in every module so the existing database or existing records having this field should be auto filled on save of above field called name.

Backend field should be auto filled by the value of above field.

backend = fields.Many2one('comodel='qb.backend')

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

do it on qb.backend create method like this.

@api.model

def create(self, vals):

    res = super(QbBackend, self).create(vals)

    self.env['YourModel'].search([('backend_id', '=', False)]).write({'backend_id': res.id})

    retrun res

Ảnh đại diện
Huỷ bỏ
Tác giả

Thank you @Jigar Vaghela (jva). It works.

Welcome mansi

also give vote to this answer so other can find it easily.

Tác giả Câu trả lời hay nhất

@api.model
    def create(self, vals):
         res = super(bk_backend, self).create(vals)
        self.env['account.analytic.line'].search([('backend_id', '=', False)]).write({'backend_id': res.id})
        return res

This worked @jigarvagela

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
2
thg 2 23
8937
2
thg 7 24
2447
1
thg 6 24
4961
1
thg 10 23
10627
1
thg 10 23
98