如何使用新的 API 为 many2many 字段正确创建、写入或取消链接记录?
How do I properly create, write or unlink records for many2many field using the new API?
有人可以给我一个使用新 API 操作 many2many 字段的例子吗?我已经尝试阅读 Documentation 但无济于事。
这是我的例子类:
from openerp import models, fields, api, _
class example_class_one(models.Model):
_name = "example.class.one"
name = fields.Char('Name')
value = fields.Float('Value')
example_class_one()
class example_class_two(models.Model):
_name = "example.class.two"
name = fields.Char('Name')
example_class_ones = fields.Many2many('example.class.one',string='Example Class Ones')
@api.one
def test(self):
#CREATES SOME example_class_ones and assign them to self
#MANIPULATE SOME example_class_ones and save them
#DELETE SOME example_class_ones from self
pass
example_class_two()
在 Odoo 8 中,新的 ORM API 比以前的更好(具有所有这些无聊的 (cr, uid, ids, ..) 参数)。这个新 API 对我来说最大的优势之一是我们现在使用 objects,而不是 ids。
新方法只需要 self
参数。您可以遍历它——除其他外,它也是 odoo 对象的集合。
还有一个魔法变量 - self.env
是环境类型,包含所有这些 cr, uid, etc.
东西。它还包含所有已知模型的集合——这就是您所需要的。
那你为什么不试试这个方法:
@api.one
def test(self):
model_one = self.env['example.class.one']
model_one.create({'name': 'Some ONE object', 'value': 2.0})
ones = model_one.browse([1, 3, 5])
ones2 = model_one.search([('name', '=', 'Some name')])
# You can imagine - search() return also objects! :)
ones2[0].unlink()
# Or, to deal with you many2many field...
self.example_class_ones += model_one.new({
'name': 'New comer to the many2many relation',
'value': 666.0})
希望能回答您的问题。
你可以参考我的案例如下,
在 @api
@api.onchange('opportunity_id')
def _get_description(self):
if self.opportunity_id.id:
self.x_description = self.opportunity_id.x_description
或声明为设置关系和字段(相关)如下link
Pass custom field values from oppertunity to quotation in odoo 10
有人可以给我一个使用新 API 操作 many2many 字段的例子吗?我已经尝试阅读 Documentation 但无济于事。
这是我的例子类:
from openerp import models, fields, api, _
class example_class_one(models.Model):
_name = "example.class.one"
name = fields.Char('Name')
value = fields.Float('Value')
example_class_one()
class example_class_two(models.Model):
_name = "example.class.two"
name = fields.Char('Name')
example_class_ones = fields.Many2many('example.class.one',string='Example Class Ones')
@api.one
def test(self):
#CREATES SOME example_class_ones and assign them to self
#MANIPULATE SOME example_class_ones and save them
#DELETE SOME example_class_ones from self
pass
example_class_two()
在 Odoo 8 中,新的 ORM API 比以前的更好(具有所有这些无聊的 (cr, uid, ids, ..) 参数)。这个新 API 对我来说最大的优势之一是我们现在使用 objects,而不是 ids。
新方法只需要 self
参数。您可以遍历它——除其他外,它也是 odoo 对象的集合。
还有一个魔法变量 - self.env
是环境类型,包含所有这些 cr, uid, etc.
东西。它还包含所有已知模型的集合——这就是您所需要的。
那你为什么不试试这个方法:
@api.one
def test(self):
model_one = self.env['example.class.one']
model_one.create({'name': 'Some ONE object', 'value': 2.0})
ones = model_one.browse([1, 3, 5])
ones2 = model_one.search([('name', '=', 'Some name')])
# You can imagine - search() return also objects! :)
ones2[0].unlink()
# Or, to deal with you many2many field...
self.example_class_ones += model_one.new({
'name': 'New comer to the many2many relation',
'value': 666.0})
希望能回答您的问题。
你可以参考我的案例如下,
在 @api
@api.onchange('opportunity_id')
def _get_description(self):
if self.opportunity_id.id:
self.x_description = self.opportunity_id.x_description
或声明为设置关系和字段(相关)如下link
Pass custom field values from oppertunity to quotation in odoo 10