Search code examples
python-3.xodoo-12

Odoo: on create save current ID in a many2one field of another model


what I'm trying to do is I'm creating a custom.modify.price model for products, where I'm able to choose a certain criteria to result in a selection of products, then I fill in a percentage that affects the prices of all the previous selection. I want it to create a new record, for each product in the selection, in a another model custom.sale.price and each record should be holding the ID of the custom.modify.price record that created it in a many2one field. The thing is I can create a new record of this custom.sale.price by overriding the custom.modify.price create method and feeding it the values to do so, but I am not able to feed it the current ID of "not yet created" custom.modify.price record

class CustomModifyPrice(models.Model):
    date = fields.Datetime(string='Date', required=True, default=fields.Datetime.now())
    brand_id = fields.Many2many(comodel_name="custom.brand", string="Brand", required=False, )
    origin_id = fields.Many2many(comodel_name="custom.country", string="Origin", required=False, )
    percentage = fields.Float(string="Percentage")
    product_ids = fields.Many2many(comodel_name="custom.product", string="Products", readonly=True, )
    
    @api.model
    def create(self, vals):
      values = {'product_id': product.id, 'date': vals['date'], 'sell_price': mod_sell_price, 'modification_id': self.id}###self.id returns nothing here
      price = self.env['custom.sale.price'].create(values)
      result = super(CustomModifyPrice, self).create(vals)
      return result

class CustomSalePrice(models.Model):
    product_id = fields.Many2one(comodel_name="custom.product", string="Product", required=False, )
    date = fields.Datetime(string="Date", default=fields.Datetime.now())
    sell_price = fields.Float(string="Selling Price")
    sale_id = fields.Many2one(comodel_name="custom.sale", string="Sales Order", required=False, )



Solution

  • You need to to create custom.modify.price and update values with the result id then create custom.sale.price

    @api.model
    def create(self, vals):
      result = super(CustomModifyPrice, self).create(vals)
      values = {'modification_id': result.id, ...}
      self.env['custom.sale.price'].create(values)      
      return result