在 Odoo/OpenERP 测试中将记录 ID 从 !python 传递到 YAML

Pass record id from !python to YAML in Odoo/OpenERP tests

我有一条记录是在 !python 块中通过调用从 Crm Lead 创建销售订单的函数创建的。现在我想在新创建的销售订单上测试工作流程。有没有办法将新的 sale_order 的 id 传回 YAML,这样我就可以使用 !workflow 指令了?

您可以将 id 保存到 !python 块内的 ir.model.data,它对其他块可见。

这是一个从 python 块创建组并从 !record 块向其添加用户的示例:

-
  Create an external id 'test_group_id'
-                                                                                                                                                                                                                                              
  !python {model: res.groups}: |                                                                                                                                                                                                               
    from openerp import SUPERUSER_ID                                                                                                                                                                                                           
    obj_id = self.create(cr, uid, {'name':'Test Group'}, context=context)                                                                                                                                                                      
    imd_values = {'module':'base', 'model': model._name, 'name': 'test_group_id', 'res_id': obj_id}                                                                                                                                            
    id = self.pool['ir.model.data'].create(cr, SUPERUSER_ID, imd_values, context=context)                                                                                                                                                      
    assert obj_id == ref('test_group_id'), 'saved reference should be equal'                                                                                                                                                                   
    assert id, 'external id should be saved'                                                                                                                                                                                                   
-
  The identifier 'test_group_id' should be visible from other "python" tags
-                                                                                                                                                                                                                                              
  !python {model: res.groups}: |
    assert ref('test_group_id'), 'External Id should exists' 
-
  Create user 'Test User' to include it in 'test_group_id'
-                                                                                                                                                                                                                                              
  !record {model: res.users, id: res_users_test, view: False}:
    company_id: base.main_company
    name: Test User
    login: testuser
    password: testuser
-
  Include the user in the referenced group. Then 'test_group_id' is visible from "record" tags.
-                                                                                                                                                                                                                                              
  !record {model: res.users, id: res_users_test}:
    groups_id:
      - base.test_group_id