【问题标题】:Get product's name in the local language in OpenERP?在 OpenERP 中获取本地语言的产品名称?
【发布时间】:2014-04-24 11:57:02
【问题描述】:

这是一个非常简单但很难回答的问题,因为我认为只有“少数”人会处理这个问题。

我在 Python 中有这个简单的脚本:

import xmlrpclib

username = 'my_openerp_user'
pwd = 'my_password'
dbname = 'my_openerp_database'

sock = xmlrpclib.ServerProxy('http://localhost:8063/xmlrpc/common')
uid = sock.login(dbname, username, pwd)
sock = xmlrpclib.ServerProxy('http://localhost:8063/xmlrpc/object')

args = [('name', 'ilike', 'my_product')]
ids = sock.execute(dbname, uid, pwd, 'product.product', 'search', args)

print ids

它应该在 OpenERP 数据库中找到名称为“my_product”的所有产品,但它没有。我知道为什么:

我不在说英语的国家,所以我在 OpenERP 中安装了一种语言,xmlrpc 的“搜索”正在寻找名称为“my_product”但只有英文的产品。问题是显然没有保存翻译名称的字段......它似乎也是“名称”!因此,如果我用我的语言指定产品名称,我将找不到产品。

世界上有人遇到过同样的问题吗?

编辑

好的,我有一个线索:如果我不进行“搜索”,而是“阅读”:

product_names = sock.execute(dbname, uid, pwd, 'product.product', 'read', ids, ['name'], {'lang': 'es_ES'})

这样我可以指定语言并且它可以工作!但是我不能对“搜索”做同样的事情,我会出错。谁知道路???

新编辑

context = {'lang': 'es_ES'}

args = [('name', 'ilike', 'my_product')]  # consulta

ids = sock.execute(dbname, uid, pwd, 'product.product', 'search', args, context)

【问题讨论】:

    标签: python odoo xmlrpclib


    【解决方案1】:

    我几乎可以肯定,如果你在 context 中传递 lang,它应该可以工作。

    例如:context = {'lang': u'pl_PL'}

    如果它不起作用,你应该尝试覆盖 name_search 方法。

    【讨论】:

    • 不是吗?看起来确实如此。查看 expression.parse 方法(在方法末尾的 else 块中)。
    • 对不起,我有点迷路了,我该去哪里找那个 expression.parse?
    • openerp/osv/expression.pyexpression 方法parse。当您调用搜索时,它会转到BaseModel._search()->BaseModel._where_calc()->expression.init()->expression.parse(),并且在此方法结束时,它会从 ir_translation 表中获取翻译。我了解到您已尝试在 context 中传递 lang,但它不起作用?
    • 当我使用与您在上面写的完全相同的变量“上下文”(使用我的语言)时,它可以工作,但仅限于“读取”和“创建”方法。如果我在 'search', context={'lang: es_ES'} TypeError: __call__() got an unexpected keyword argument 'context' 中将 'context' 作为关键字参数传递,我会收到此错误
    • 你能告诉我你怎么称呼搜索吗?看起来很奇怪。你能显示整个堆栈跟踪吗?
    【解决方案2】:

    搜索方法是这样的:

    search(cr, uid, args, offset=0, limit=None, order=None, context=None, count=False)
    

    如果要发送上下文,还必须在 args 和上下文之间发送参数:

    ids = sock.execute(dbname, uid, pwd, 'product.product', 'search', args, 0, 0, False, context)
    

    【讨论】:

      【解决方案3】:

      在 openerp/osv/expression.py 中,查询已重新制定,仅当 ir_translation 中不存在翻译时,才首先搜索英文原名的翻译。 OpenERP 服务器 6.0.4

      query1 = '( SELECT res_id'          \
               '    FROM ir_translation'  \
               '   WHERE name = %s'       \
               '     AND lang = %s'       \
               '     AND type = %s'
      instr = ' %s'
      #Covering in,not in operators with operands (%s,%s) ,etc. 
      if operator in ['in','not in']:
          instr = ','.join(['%s'] * len(right))
          query1 += '     AND value ' + operator +  ' ' +" (" + instr + ")" + ')'
      else:
          query1 += '     AND value ' + operator + instr + ')'
      query1 +=' UNION ('                \ 
               '  SELECT id'              \
               '    FROM "' + working_table._table + '"' + ' as wt '   \
               '   WHERE "'+ left + '" ' + operator + instr +  \
               '   AND NOT EXISTS (   ' \   
               '       SELECT res_id FROM ir_translation ' \
               '       WHERE wt.id = res_id ' \
               '       AND name = %s'       \
               '       AND lang = %s'       \
               '       AND type = %s)' +  ")"
      
      
      query2 = [working_table._name + ',' + left,
                context.get('lang', False) or 'en_US',
                'model',
                right,
                right,
                working_table._name + ',' + left,
                context.get('lang', False) or 'en_US',
                'model'
                 ]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-11-02
        • 1970-01-01
        • 2020-09-11
        • 1970-01-01
        • 2019-10-10
        • 1970-01-01
        相关资源
        最近更新 更多