Skip to content

Instantly share code, notes, and snippets.

@repodevs
Created August 24, 2017 05:59
Show Gist options
  • Save repodevs/f6015cdc8fca75bdc473167be7b06a67 to your computer and use it in GitHub Desktop.
Save repodevs/f6015cdc8fca75bdc473167be7b06a67 to your computer and use it in GitHub Desktop.
an ugly method, to constrains odoo default_code (and name)
# -*- coding: utf-8 -*-
# © 2017 Edi Santoso <[email protected]>
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html)
from odoo import api, fields, models, _
class Product(models.Model):
_inherit = 'product.template'
@api.constrains('default_code', 'name')
def _check_code(self):
if self.default_code or self.name:
exi = self.env['product.template'].search_count(['|',('name', '=', self.name),('default_code', '=', self.default_code)])
if exi > 1:
raise ValueError(_("Code / Name already used !!!"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment