使产品名称具有唯一性

时间:2015-03-26 15:33:13

标签: python openerp-7

我有一个问题 当我尝试覆盖product.product使其名称是唯一的 我添加它

import logging
from openerp.osv import fields, osv
import time
import openerp.addons.decimal_precision as dp
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
from openerp.tools.translate import _
from openerp import netsvc
from datetime import datetime, date

    class product_override(osv.osv):
        _name = 'product.product'
        _inherit = 'product.product'
        _columns = {

                    'min_charge' : fields.float(_('Min.Charge')),
                    }

        _sql_constraints = [ ('name_uniq_1', 'unique(name)', _('Name for Product must be unique!')), ]

但它不起作用。有帮助吗?我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

product.product 是通过委派继承自 product.template 的。 product.template 中存在名称字段,而不是 product.product

如果你查看日志,你会得到 openerp.sql_db: Programming error: column "name" named in key does not exist, in query ALTER TABLE "product_product" ADD CONSTRAINT "product_product_name_uniq_1" unique(name)

将您的代码更改为

import logging
from openerp.osv import fields, osv
import time
import openerp.addons.decimal_precision as dp
from openerp.tools import DEFAULT_SERVER_DATE_FORMAT
from openerp.tools.translate import _
from openerp import netsvc
from datetime import datetime, date

class product_template_override(osv.osv):
    _name = 'product.template'
    _inherit = 'product.template'
    _sql_constraints = [ ('name_uniq_1', 'unique(name)', _('Name for Product must be unique!')) ]

class product_override(osv.osv):
        _name = 'product.product'
        _inherit = 'product.product'
        _columns = {

                    'min_charge' : fields.float(_('Min.Charge')),
                    }