【问题标题】:How to get inactive records in One2many/Many2many fields?如何在 One2many/Many2many 字段中获取非活动记录?
【发布时间】:2018-01-23 12:30:34
【问题描述】:

我们以One2many为例,更清晰:想象一个人可以有几只宠物,但一只宠物只能有一个主人:

class Pet(models.Model):
    _name='pet'

    owner_id = fields.Many2one(
        comodel_name='person',
        string='Owner',
    )
    active = fields.Boolean(
        string='Active',
        default=True,
    )

class Person(models.Model):
    _name='person'

    pet_ids = fields.One2many(
        comodel_name='pet',
        inverse_name='owner_id',
        string='Pets',
    )

现在您有一个人 (Id: 1),他有两只宠物 (Ids: 56, 57),但其中一只不活跃 (一个 ID:57)。如果您打印person.pet_ids,Odoo 将返回pet(56,)。非活动记录不包括在内。打印person.pets_ids时有什么方法可以显示吗?

到目前为止我是这样做的:

pets = self.env['pet'].search([
    ('owner_id', '=', person.id),
    '|',
    ('active', '=', True),
    ('active', '=', False),
])

但我想知道是否有更好的方法。

【问题讨论】:

    标签: python python-2.7 odoo-8 odoo


    【解决方案1】:

    你可以通过

    {'active_test': False}
    

    在上下文中。在你的情况下:

    pets = self.env['pet'].with_context(active_test=False).search([('owner_id', '=', person.id)])
    

    【讨论】:

    • 您的解决方案也适用于记录集,因此更加简单:person.with_context({'active_test': False, }).pet_ids。非常感谢@phogl!
    猜你喜欢
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多