需要解释语法和使用搜索/浏览返回变量

时间:2015-03-26 13:16:29

标签: python odoo

这可能部分是一个蟒蛇问题,但我仍然认为我需要把这个问题拉进来。

这是我的代码。我只是从另一件作品中复制了它,却没有完全理解它会做什么(我以为我做了......)

sale_line_ids = self.pool.get('sale.order.line').search(cr, uid, [('order_id', '=', order_id)], context=context)
print sale_line_ids
print 'that was sale_line_ids'
for sale_line_id in self.pool.get('sale.order.line').browse(cr, uid, sale_line_ids, context=context):
    print sale_line_id
    print 'that was one sale_line_id'
    print sale_line_id.product_id
    print 'that was one the product id'
    product_obj = self.pool.get('product.template').browse(cr, uid, sale_line_id.product_id, context=context)
        print product_obj.name

这就是结果,穿插着我的问题:

[6]
that was sale_line_ids 

这是预期的,返回的变量只是一个python列表。

sale.order.line(6,) 
that was one sale_line_id 

现在,这是什么?它看起来像缺少第二个参数的函数。这肯定不能是sale_line_id变量的内容?现在它变得更加奇怪了,因为在代码中访问变量sale_line_id.product_id可能意味着sale.order.line(6,).product_id,它再次打印出类似的奇怪文本字符串:

product.product(2,) 
that was one the product id

好的,我没有得到它,但它与之前的结果相似。在此之后我得到一个堆栈跟踪,结束于:

  File "/opt/odoo/openerp/api.py", line 241, in wrapper
    return old_api(self, *args, **kwargs)
  File "/opt/p711/odoo-addons/p711/models.py", line 67, in paypal_form_generate_values
    print product_obj.name
  File "/opt/odoo/openerp/fields.py", line 817, in __get__
    self.determine_value(record)
  File "/opt/odoo/openerp/fields.py", line 910, in determine_value
    record._prefetch_field(self)
  File "/opt/odoo/openerp/api.py", line 239, in wrapper
    return new_api(self, *args, **kwargs)
  File "/opt/odoo/openerp/models.py", line 3211, in _prefetch_field
    result = records.read(list(fnames), load='_classic_write')
  File "/opt/odoo/openerp/api.py", line 239, in wrapper
    return new_api(self, *args, **kwargs)
  File "/opt/odoo/openerp/models.py", line 3156, in read
    self._read_from_database(stored, inherited)
  File "/opt/odoo/openerp/api.py", line 239, in wrapper
    return new_api(self, *args, **kwargs)
  File "/opt/odoo/openerp/models.py", line 3279, in _read_from_database
    cr.execute(query_str, [tuple(sub_ids)] + where_params)
  File "/opt/odoo/openerp/sql_db.py", line 158, in wrapper
    return f(self, *args, **kwargs)
  File "/opt/odoo/openerp/sql_db.py", line 234, in execute
    res = self._obj.execute(query, params)
  File "/home/odoo/odoo/lib/python2.7/site-packages/psycopg2/extensions.py", line 129, in getquoted
    pobjs = [adapt(o) for o in self._seq]
ProgrammingError: can't adapt type 'product.product'

显然是尝试访问product_obj.name的结果,但现在我迷失了,有人可以给我一些洞察力,也许可以解释一下如何以及为什么这样做?

1 个答案:

答案 0 :(得分:0)

搜索为您提供ID列表,浏览为您提供可浏览的记录,如product.product(2,)。

只需将1行更改为

product_obj = self.pool.get('product.template').browse(cr, uid, sale_line_id.product_id.id, context=context)

因为browse方法将id作为参数而不是可浏览对象。 sale_line_id.product_id为您提供了一个不是ID列表的对象,因此我只更改为sale_line_id.product_id.id

sale_line_id.product_id它为您提供了可浏览的对象。 这个product.product(2,)实际列表中没有任何内容,元组,字典在最后一个元素之后可以有额外的逗号。

您还将获得此类产品的字段

sale_line_id.product_id.name