Openerp 以ManyOne而不是name显示另一个字段
我有很多这样的领域Openerp 以ManyOne而不是name显示另一个字段,openerp,Openerp,我有很多这样的领域 'product_id': fields.many2one('product.product', 'Product', required=True) 它向我显示所有产品名称(product_template类中的name属性)并在其中存储id。我想显示产品的零件号,而不是里面的名称和存储id。在openerp7中是否有这样做的方法。谢谢您的时间。当您使用浏览方法时,您将获得产品的所有信息数据 如果获得属性,请使用: <yourobject>.product_id
'product_id': fields.many2one('product.product', 'Product', required=True)
它向我显示所有产品名称(product_template类中的name属性)并在其中存储id。我想显示产品的零件号,而不是里面的名称和存储id。在openerp7中是否有这样做的方法。谢谢您的时间。当您使用浏览方法时,您将获得产品的所有信息数据 如果获得属性,请使用:
<yourobject>.product_id.name
<yourobject>.product_id.default_code
<yourobject>.product_id.type
<yourobject>.product_id.<your_property>
.product\u id.name
.product\u id.default\u代码
.产品标识类型
.产品标识。
我希望你用。关于有几种方法可以做到这一点 为关系字段显示的名称是
name\u get
方法的结果。您可以继承product.product
并覆盖name\u get
,但这将影响整个系统,每次显示产品时都会更改此设置。您可以使用上下文来编码,但它开始变得混乱起来
如果您只需要代码,我会创建一个类似这样的相关字段
'product_ref': fields.related(
'product_id',
'part_number',
string='Product',
readonly=True,
type='char',
help = 'The product part number',
)
然后在你的表格上用这个。ORM层足够智能,如果您的特定记录没有产品ID或产品没有零件号,它将优雅地处理它
如果您需要更巧妙一些,您可以创建一个显示名称和零件号的功能字段。我的基本意图是显示所有产品的零件号,并提供按零件号搜索的功能
我在product.product类中以这种方式完成了它,(Vivek和End之间的代码是我的代码片段)
def name_get(self、cr、user、id、context=None):
如果上下文为“无”:
上下文={}
如果isinstance(ID,(int,long)):
ids=[ids]
如果不是len(ID):
返回[]
定义名称(d):
name=d.get('name','')
code=d.get('default_code',False)
#维韦克
零件号=d.get('零件号',False)
如果零件号:
名称=“%s-%s%”(名称,零件号)
#结束
elif代码:
名称=“[%s]%s%”(代码,名称)
elif d.get('variants'):
名称=名称+'-%s'%(d['variants'],)
返回(d['id'],名称)
partner\u id=context.get('partner\u id',False)
结果=[]
对于self.browse中的产品(cr、用户、ID、context=context):
卖家=过滤器(lambda x:x.name.id==合作伙伴\u id,产品.seller\u id)
#维韦克
prd_temp=self.pool.get('product.template')。浏览(cr,用户,product.id,context=context)
#结束
如果卖方:
对于卖方中的s:
mydict={
“id”:product.id,
“name”:s.product\u name或product.name,
#维韦克
“零件号”:珠三角临时零件号,
#结束
“默认代码”:s.product\U代码或product.default\U代码,
“变体”:product.variants
}
result.append(_name_get(mydict))
其他:
mydict={
“id”:product.id,
“名称”:product.name,
#维韦克
“零件号”:珠三角临时零件号,
#结束
“默认\u代码”:product.default\u代码,
“变体”:product.variants
}
result.append(_name_get(mydict))
返回结果
定义名称搜索(self,cr,user,name='',args=None,operator='ilike',context=None,limit=100):
如果不是args:
args=[]
如果名称:
id=self.search(cr,用户,[('default_code','=',name)]+args,limit=limit,context=context)
如果没有ID:
ids=self.search(cr,用户,[('ean13','=',名称)]+args,limit=limit,context=context)
如果没有ID:
#不要将接下来的两行合并到一个搜索中,SQL搜索性能将非常糟糕
#在一个拥有数千个匹配产品的数据库上,由于
#或运算符(并且假定“名称”查找结果来自ir.translation表)
#在Python中执行ID的快速内存合并将提供更好的性能
ids=set()
id.update(self.search(cr,user,args+[('default_code',operator,name)],limit=limit,context=context))
如果没有限制或长度(ID)<限制:
#我们可能会因为结果中的重复而低于极限,这很好
更新(self.search(cr,user,args+[('name',operator,name)],limit=(limit and(limit len(id))或False),context=context))
#维韦克
#用途:使用零件号过滤产品
id.update(self.search(cr,用户,args+[('part_number',operator,name)],limit=(limit and(limit len(ids))或False),context=context))
#结束
ids=列表(ids)
如果没有ID:
ptrn=re.compile('(\[(.*?\]))
res=ptrn.search(名称)
如果有的话:
ids=self.search(cr,用户,[('default_code','=',res.group(2))]+args,limit=limit,context=context)
其他:
ids=self.search(cr、用户、参数、限制=limit、上下文=context)
结果=self.name\u get(cr、用户、ID、上下文=上下文)
返回结果
但是这个特殊功能的问题是“这是一个全球性的变化”,无论你在哪里列出产品,都会显示产品名称-零件号。如果有人能为特定环境的操作做得更好,那就太好了
欢迎进一步的回答。谢谢Adrian,它真的很好,很有用。但是,我的目的是从现有产品列表中搜索零件号(而不是名称,希望按
def name_get(self, cr, user, ids, context=None):
if context is None:
context = {}
if isinstance(ids, (int, long)):
ids = [ids]
if not len(ids):
return []
def _name_get(d):
name = d.get('name','')
code = d.get('default_code',False)
# Vivek
part_number = d.get('part_number',False)
if part_number:
name = '%s-%s' % (name,part_number)
#End
elif code:
name = '[%s] %s' % (code,name)
elif d.get('variants'):
name = name + ' - %s' % (d['variants'],)
return (d['id'], name)
partner_id = context.get('partner_id', False)
result = []
for product in self.browse(cr, user, ids, context=context):
sellers = filter(lambda x: x.name.id == partner_id, product.seller_ids)
# Vivek
prd_temp = self.pool.get('product.template').browse(cr, user, product.id, context=context)
# End
if sellers:
for s in sellers:
mydict = {
'id': product.id,
'name': s.product_name or product.name,
#vivek
'part_number': prd_temp.part_number,
#End
'default_code': s.product_code or product.default_code,
'variants': product.variants
}
result.append(_name_get(mydict))
else:
mydict = {
'id': product.id,
'name': product.name,
#vivek
'part_number': prd_temp.part_number,
#End
'default_code': product.default_code,
'variants': product.variants
}
result.append(_name_get(mydict))
return result
def name_search(self, cr, user, name='', args=None, operator='ilike', context=None, limit=100):
if not args:
args = []
if name:
ids = self.search(cr, user, [('default_code','=',name)]+ args, limit=limit, context=context)
if not ids:
ids = self.search(cr, user, [('ean13','=',name)]+ args, limit=limit, context=context)
if not ids:
# Do not merge the 2 next lines into one single search, SQL search performance would be abysmal
# on a database with thousands of matching products, due to the huge merge+unique needed for the
# OR operator (and given the fact that the 'name' lookup results come from the ir.translation table
# Performing a quick memory merge of ids in Python will give much better performance
ids = set()
ids.update(self.search(cr, user, args + [('default_code',operator,name)], limit=limit, context=context))
if not limit or len(ids) < limit:
# we may underrun the limit because of dupes in the results, that's fine
ids.update(self.search(cr, user, args + [('name',operator,name)], limit=(limit and (limit-len(ids)) or False) , context=context))
# vivek
# Purpose : To filter the product by using part_number
ids.update(self.search(cr, user, args + [('part_number',operator,name)], limit=(limit and (limit-len(ids)) or False) , context=context))
#End
ids = list(ids)
if not ids:
ptrn = re.compile('(\[(.*?)\])')
res = ptrn.search(name)
if res:
ids = self.search(cr, user, [('default_code','=', res.group(2))] + args, limit=limit, context=context)
else:
ids = self.search(cr, user, args, limit=limit, context=context)
result = self.name_get(cr, user, ids, context=context)
return result