Search code examples
pythonpython-2.7odoo-8odoo

How to get inactive records in One2many/Many2many fields?


Let's put an example with an One2many, which is clearer: imagine that a person can have several pets, but a pet can only have an owner:

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',
    )

Now you have a person (Id: 1) who has two pets (Ids: 56, 57), but one of them is inactive (the one with Id: 57). If you print person.pet_ids, Odoo returns pet(56,). Inactive records are not included there. Is there any way to show them when printing person.pets_ids?

I did this so far:

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

But I was wondering if there is a better way.


Solution

  • You can pass

    {'active_test': False}
    

    in the context. In your case:

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