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

Hello


How to Override/Replace create function NOT extending (adding onto)


Thanks in advance

อวตาร
ละทิ้ง
คำตอบที่ดีที่สุด

Hi, Yazeed


There have two ways to override create(ORM functions).

  • Call inherited function as well as other which are inherit before,

    • class CustomModel(models.Model):
      _inherit = "custom.model"

      @api.model
      def create(self, vals):
      return super(CustomModel, self).create(vals)
    • It will call current method as well as all the create methods which are inherited before.

  • Call self and Models.model definition,

    • class CustomModel(models.Model):
      _inherit = "custom.model"

      @api.model
      def create(self, vals):
      ## Definition
      return super(models.Model, self).create(vals)
       Here only this function and create function of "model.Model" will call
Thanks,
Ashish Singh (Team Lead)
Webkul Software Private Limited
อวตาร
ละทิ้ง
ผู้เขียน

Thanks Ashish, solved

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

Dear Yazeed,

class ModelModel1(models.Model):

    _name = "table.table"

    _description = "Test"

    @api.multi

    def test_method(self):

        {fuction definition}

    @api.model

    def create(self, vals):

    vals['number'] = self.env['ir.sequence'].next_by_code('annual.achievement.request.seq')

    val['field1'] = 'name'

    val['field2'] = 'age'

    rec = super(ModelModel1, self).create(vals)

    rec.test_method()

    return rec

As like you can change any value or add value to the create function. also, you can call external methods also.

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

Thanks Nikhil

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

Hi,

Check it out https://www.odoo.com/forum/help-1/question/write-and-create-function-130691

Thanks

อวตาร
ละทิ้ง