3

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.

forvas
  • 9,801
  • 7
  • 62
  • 158

1 Answers1

6

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)])
Naglis
  • 2,583
  • 1
  • 19
  • 24
phogl
  • 494
  • 1
  • 8
  • 16
  • 2
    Your solution also works with recordsets, so it's even simplier: `person.with_context({'active_test': False, }).pet_ids`. Thank you very much @phogl! – forvas Jan 23 '18 at 15:54