私はこのようなmany2oneフィールドを持っています
'product_id': fields.many2one('product.product', 'Product', required=True)
すべての製品名 (product_template クラスの name 属性) が表示され、id が内部に格納されます。商品名や店舗IDの代わりに品番を表示したいのですが。openerp7 でこれを行う方法はありますか。御時間ありがとうございます。
私はこのようなmany2oneフィールドを持っています
'product_id': fields.many2one('product.product', 'Product', required=True)
すべての製品名 (product_template クラスの name 属性) が表示され、id が内部に格納されます。商品名や店舗IDの代わりに品番を表示したいのですが。openerp7 でこれを行う方法はありますか。御時間ありがとうございます。
これを行うにはいくつかの方法があります。
リレーショナル フィールドに表示される名前は、name_get
メソッドの結果です。継承product.product
と上書きはできますname_get
が、それはシステム全体に影響し、製品が表示されるたびにこれが変更されます。を使用してこれを回避するコードを作成できますが、context
面倒になり始めています。
コードだけが必要な場合は、このような関連フィールドを作成します。
'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, 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
しかし、この特定の機能の問題は、製品を一覧表示する場所が「製品名 - 部品番号」と表示される「グローバルな変更」です。コンテキスト固有のアクションに対して誰かがそれをよりうまく行うことができれば、それは素晴らしいことです。
さらなる回答をお待ちしております。:)