Search code examples
odooodoo-14odoo-15

Move Many2one variable to another Model


I have 2 models "Inventory" and "Scrap". There is a button. I want to, if i click the button the data move to scrap model. It is okay i did that. But except one variable, "Many2one".

So I'm getting this error, when i click the button: "psycopg2.ProgrammingError: can't adapt type 'ware.houses' odoo".

This is my Inventory Module.

class InventoryMenu(models.Model):
    _name = "inventory.menu"
    _description = "Inventory Menü"

    ref_code = fields.Char(string="Referans Numarası: ", required=True, tracking=True, default="000000")
    product_name = fields.Char(string="Ürün Adı: ", required=True, tracking=True,)
    product_description = fields.Char(string="Ürün Tanımı: ", required=True, tracking=True,)
    teslim_alan = fields.Char(string="Teslim Alan: ", required=True, tracking=True,)
    teslim_eden = fields.Char(string="Teslim Eden: ", required=True, tracking=True,)
    quantity = fields.Float(string="Miktar: ", required=True, tracking=True,)
    price = fields.Float(string="Fiyat: ", required=True, tracking=True,)
    unit_price = fields.Float(string="Birim Fiyat: ", compute="_unitPriceCalcuteFunc")
    warehouse_id = fields.Many2one('ware.houses', string='Depo Adı: ')

    @api.depends('quantity', 'price')
    def _unitPriceCalcuteFunc(self):
        for record in self:
            record.unit_price = record.price * record.quantity

    def action_delete(self):
        vals = {
            'ref_code': self.ref_code,
            'product_name': self.product_name,
            'product_description': self.product_description,
            'teslim_alan': self.teslim_alan,
            'teslim_eden': self.teslim_eden,
            'quantity': self.quantity,
            'price': self.price,
            'unit_price': self.unit_price,
            'warehouse_id': self.warehouse_id
        }
        self.env['scrap'].create(vals)
        return super(InventoryMenu, self).unlink()

This is my Scrap module:

class Scrap(models.Model):
    _name = "scrap"
    _description = "Scrap"

    ref_code = fields.Char(string="Referans Numarası: ", required=True, tracking=True, default="000000")
    product_name = fields.Char(string="Ürün Adı: ", required=True, tracking=True, )
    product_description = fields.Char(string="Ürün Tanımı: ", required=True, tracking=True, )
    teslim_alan = fields.Char(string="Teslim Alan: ", required=True, tracking=True, )
    teslim_eden = fields.Char(string="Teslim Eden: ", required=True, tracking=True, )
    quantity = fields.Float(string="Miktar: ", required=True, tracking=True, )
    price = fields.Float(string="Fiyat: ", required=True, tracking=True, )
    unit_price = fields.Float(string="Birim Fiyat: ", compute="_unitPriceCalcuteFunc")
    warehouse_id = fields.Many2one('ware.houses', string='Depo Adı: ')

    @api.depends('quantity', 'price')
    def _unitPriceCalcuteFunc(self):
        for record in self:
            record.unit_price = record.price * record.quantity

    def action_getback(self):
        vals = {
            'ref_code': self.ref_code,
            'product_name': self.product_name,
            'product_description': self.product_description,
            'teslim_alan': self.teslim_alan,
            'teslim_eden': self.teslim_eden,
            'quantity': self.quantity,
            'price': self.price,
            'unit_price': self.unit_price,
            'warehouse_id': self.warehouse_id
        }
        self.env['inventory.menu'].create(vals)
        return super(Scrap, self).unlink()


Solution

  • Here is the solution for above code:

    'warehouse_id': self.warehouse_id
    

    Please change this line to:

    'warehouse_id': self.warehouse_id.id
    

    While creating your vals in action_delete and action_getback methods.

    Root Cause: When you pass dictionary of values which you want to create in your model, make sure that every field have correct value.

    For EX: If field is Many2one than you need to pass the record_id (type=int), because odoo will directly browse the id to the related model and place the record to that field.