Search code examples
odooodoo-14odoo-15

Calling function from another model in wizard, Odoo


I can call the function from another model in wizard. But there is a problem. When i call the function from another model, I can't use the fields.

Calling function code:

self.env['inventory.menu'].action_delete()

Function from another model:

    def action_delete(self):
        print("girdi", self.price)
        vals = {
            'ref_code': self.ref_code,
            'products_id': self.products_id.id,
            'product_description': self.product_description,
            'teslim_alan': self.teslim_alan,
            'teslim_eden': self.teslim_eden,
            'quantity': self.scrap_quantity,
            'price': self.price,
            'unit_price': self.unit_price,
            'warehouse_id': self.warehouse_id.id
        }
        self.env['scrap'].create(vals)

I'm getting this error:

The operation cannot be completed:
- Create/update: a mandatory field is not set.
- Delete: another model requires the record being deleted. If possible, archive it instead.

How can i solve that? Thanks..


Solution

  • You're calling it on an empty recordset (self.env['inventory.menu']). If you want to use values of a record/instance, you probably should get one first.

    Let's assume you have a Many2one field on a wizard like in your other question and you press a button on that wizard, calling the wizards method action_do:

    def action_do(self):
        # self is the wizard record/instance
        # do other things before
        self.inventory_id.action_delete()
        # do other things afterwards
    

    Also have in mind, if calling on multi recordsets (one2many, many2many) always think about looping about the records. Either in your action method or in an outer scope.