关于write()方法的Odoo检查many2many关系

时间:2017-06-15 13:23:18

标签: python-2.7 many-to-many openerp odoo-9 m2m

我有2个班级:

class my_request(models.Model):

    _name = 'my.request'
    _inherit = ['mail.thread', 'ir.needaction_mixin']

    supply_ids  = fields.Many2many(comodel_name='supply.conditions',
                                              relation='purchase_supply_rel',
                                              column1='purchase_requests_id', column2='supply_conditions_id',
                                              string='Supply Conditions')

class SupplyConditions(models.Model):
    _name = 'supply.conditions'

vendor = fields.Char(string='Vendor', required=True)
quantity = fields.Char(string='Quantity', required=True)
request_id = fields.Many2one('my.request', 'My request')
name = fields.Many2one('my.request.line', string='Product', required=True)
currency_id = fields.Many2one('res.currency', string='Currency', default=_get_default_currency)
supply_rel_id = fields.Boolean(string='Relation field', default=False)
表单视图xml的

my.request类部分:

<page string="Order">
      <field name="supply_ids" domain="[('purchase_id', '=', id)]"/>
</page>

当我从my.request表单视图中选择值并单击Save按钮时,我调用write()方法,并在此时将m2m值(如果有选定值)插入关系表(purchase_supply_rel)。

当我点击“保存”按钮时,我想添加检查purchase.request id是否在purchase_requests_id字段中的purchase_supply_rel表中。我的功能。 #我的新代码之后的变化:

@api.multi
def write(self, vals):
    res = super(PurchaseRequest, self).write(vals)
    for request in self:
        if request.state != 'draft':
        if vals.get('assigned_to'):
            self.message_subscribe_users(user_ids=[request.assigned_to.id])
    # my new code
    test = self.env['purchase.request'].search([(self.id,'in','supply_ids')])
    _logger.warning("test " + str(test));
    return res

但我收到了一个错误:

  File "/opt/odoo/openerp/osv/expression.py", line 586, in check_leaf
    raise ValueError("Invalid leaf %s" % str(self.leaf))
ValueError: Invalid leaf (348, 'in', 'supply_ids')

我还能怎么检查这个?我做错了什么?

更新:

我需要像这样得到测试值smth:purchase_request(245,352) 245,352 - purchase.request类的ID

1 个答案:

答案 0 :(得分:2)

您无法在域中添加动态值来代替字段名称。 域是元组列表。域包含元组中的三个部分。

<强>域

[('field_name','operator','values')]

因此,域中的字段名称不允许使用动态值。

@api.multi
def write(self, vals):
    res = super(PurchaseRequest, self).write(vals)
    for request in self:
        if request.state != 'draft':
        if vals.get('assigned_to'):
            self.message_subscribe_users(user_ids=[request.assigned_to.id])
    # my new code
    test = self.env['purchase.request'].search([('supply_ids','in',self.ids)])
    _logger.warning("test " + str(test));
    return res