Python 如何防止odoo创建重复字段?
我在Odoo 12中创建了一个方法_sql_constraints,以使产品名称(计算字段)唯一,并且我没有得到任何重复名称的验证 这是密码Python 如何防止odoo创建重复字段?,python,sql,constraints,odoo,odoo-12,Python,Sql,Constraints,Odoo,Odoo 12,我在Odoo 12中创建了一个方法_sql_constraints,以使产品名称(计算字段)唯一,并且我没有得到任何重复名称的验证 这是密码 from odoo import api, fields, models, _ from odoo.exceptions import UserError, Warning, ValidationError from statistics import mean class autopart(models.Model): _inherit = 'prod
from odoo import api, fields, models, _
from odoo.exceptions import UserError, Warning, ValidationError
from statistics import mean
class autopart(models.Model):
_inherit = 'product.template'
@api.multi
@api.depends('item','dsc', 'drc', 'org','car','model', 'manf','year')
def naming(self):
for rec in self:
if rec.year:
rec.name = " ".join(
[rec.item or "", rec.drc or "", rec.dsc or "",
rec.org or "", rec.manf or "", rec.car or "",
rec.model or "", rec.year or ""
])
else:
rec.name = " ".join(
[rec.item and rec.item or "", rec.drc and rec.drc or "", rec.dsc and rec.dsc or "",
rec.org and rec.org or "", rec.manf and rec.manf or "", rec.car and rec.car or "",
rec.model or "",
])
_sql_constraints = [
('model', 'unique(model)', ' model must be unique'),]
name = fields.Char(string="Name", compute=naming, store=True, required=True,)
item = fields.Char( store=True, string="Item", ondelete='restrict', required=False, )
dsc = fields.Char(store=True, string="Description", ondelete='restrict', required=False)
drc = fields.Char(store=True, string="Direction", ondelete='restrict', required=False)
org = fields.Char(store=True, string="Origin", ondelete='restrict', required=True)
manf = fields.Char(store=True, string="Origin manufacture", ondelete='restrict', required=False)
car = fields.Char(store=True, string="Car", ondelete='restrict', required=False,)
year = fields.Char(store=True, string="Year", required=False, ondelete='restrict')
model = fields.Char(string="Model", ondelete='restrict', required=True, default='')
如果数据库中存在重复的产品,则以下约束将不起作用,因为存在
psycopg2.IntegrityError
_sql_constraints = [
('uniq_name', 'unique(name)', 'Name must be unique'),
]
日志:
ERROR stackoverflow odoo.sql_db: bad query: ALTER TABLE "product_template" ADD CONSTRAINT "product_template_uniq_name" unique(name)
ERROR: could not create unique index "product_template_uniq_name"
DETAIL: Key (name)=( US K360) is duplicated.
WARNING stackoverflow odoo.schema: Table 'product_template': unable to add constraint 'product_template_uniq_name'!
If you want to have it, you should update the records and execute manually:
ALTER TABLE "product_template" ADD CONSTRAINT "product_template_uniq_name" unique(name)
Traceback (most recent call last):
File "/home/odoo/odoo-12.0/odoo/tools/sql.py", line 138, in add_constraint
cr.execute(query1)
File "/home/odoo/odoo-12.0/odoo/sql_db.py", line 148, in wrapper
return f(self, *args, **kwargs)
File "/home/odoo/odoo-12.0/odoo/sql_db.py", line 225, in execute
res = self._obj.execute(query, params)
psycopg2.IntegrityError: could not create unique index "product_template_uniq_name"
DETAIL: Key (name)=( US K360) is duplicated.
错误stackoverflow odoo.sql\u db:错误查询:ALTER表“产品模板”添加约束“产品模板”唯一(名称)
错误:无法创建唯一索引“product\u template\u uniq\u name”
详细信息:键(名称)=(US K360)重复。
警告stackoverflow odoo.schema:表“product\u template”:无法添加约束“product\u template\u uniq\u name”!
如果您想拥有它,您应该更新记录并手动执行:
更改表“产品模板”添加约束“产品模板统一名称”唯一(名称)
回溯(最近一次呼叫最后一次):
文件“/home/odoo/odoo-12.0/odoo/tools/sql.py”,第138行,添加约束
cr.execute(查询1)
文件“/home/odoo/odoo-12.0/odoo/sql_db.py”,第148行,在包装器中
返回f(自,*args,**kwargs)
文件“/home/odoo/odoo-12.0/odoo/sql_db.py”,第225行,执行
res=self.\u obj.execute(查询,参数)
psycopg2.IntegrityError:无法创建唯一索引“产品模板名称”
详细信息:键(名称)=(US K360)重复。
相反,您可以使用。尝试使用此选项进行更改,
您希望产品名称应该是唯一的,请检查\u sql\u约束,并使用计算检查字段声明
_sql_constraints = [
('name_uniq', 'unique (name)', "Name already exists !"),
]
name=fields.Char(string=“name”,compute=“naming”,store=True,required=True,)
谢谢我的回答对你有帮助吗?