需要解释 search/browse return 变量的语法和用法

Need explanation of syntax and use of search/browse return variables

这可能部分是一个 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 的结果,但现在我迷路了,有人可以给我一些见解,并解释一下如何以及为什么要这样做吗?

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

只需将 1 行更改为

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

因为浏览方法将 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