How to override the create and update method of a model?

  • language
      English
  • type
      technical
  • info created for version
      14.0
  • mvc
      Python (models/controllers)
Add to Cart


    @api.model
    def create(self, vals):
        return super(ProductTemplate, self).create(self.compute_seo_url(vals))

    def write(self, vals):
        return super(ProductTemplate, self).write(self.compute_seo_url(vals))

    def compute_seo_url(self, vals):
        name = self.name
        if vals.get('name'):
            name = vals['name']
        public_categ_ids = self.public_categ_ids
        if vals.get('public_categ_ids'):
            public_categ_ids = vals['public_categ_ids'][0][2]
            if len(public_categ_ids) > 0:
                # If an id is found, fetch the public_categ_ids object ...
                public_categ_ids = self.env['product.public.category'].browse(public_categ_ids)

        if name or public_categ_ids:
            public_categ_name = ''
            if public_categ_ids:
                public_categ_name = public_categ_ids[0].name.replace('/', '-').replace(' ', '-').lower() + "-"
            vals['seo_url'] = public_categ_name + slugify(name).strip().strip('-')

        return vals