changeset 506360117958 in trytond:5.6 details: https://hg.tryton.org/trytond?cmd=changeset&node=506360117958 description: Include records without link with negative search on xxx2many
issue10178 review345981002 (grafted from c14f51b77b304a17ac9c58eb2157f86a704851cd) diffstat: trytond/model/fields/many2many.py | 5 ++++- trytond/model/fields/one2many.py | 2 ++ trytond/tests/test_field_many2many.py | 30 ++++++++++++++++++++++++++++++ trytond/tests/test_field_one2many.py | 30 ++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletions(-) diffs (121 lines): diff -r 57d3aec52a0e -r 506360117958 trytond/model/fields/many2many.py --- a/trytond/model/fields/many2many.py Sat Apr 24 19:27:07 2021 +0200 +++ b/trytond/model/fields/many2many.py Sun Apr 25 17:56:39 2021 +0200 @@ -453,7 +453,10 @@ relation_domain, tables=relation_tables) query_table = convert_from(None, relation_tables) query = query_table.select(origin, where=expression) - return table.id.in_(query) + expression = table.id.in_(query) + if operator.startswith('!') or operator.startswith('not '): + expression |= ~table.id.in_(relation.select(origin)) + return expression def definition(self, model, language): encoder = PYSONEncoder() diff -r 57d3aec52a0e -r 506360117958 trytond/model/fields/one2many.py --- a/trytond/model/fields/one2many.py Sat Apr 24 19:27:07 2021 +0200 +++ b/trytond/model/fields/one2many.py Sun Apr 25 17:56:39 2021 +0200 @@ -342,6 +342,8 @@ if operator == 'not where': expression = ~expression + elif operator.startswith('!') or operator.startswith('not '): + expression |= ~table.id.in_(target.select(origin)) return expression def definition(self, model, language): diff -r 57d3aec52a0e -r 506360117958 trytond/tests/test_field_many2many.py --- a/trytond/tests/test_field_many2many.py Sat Apr 24 19:27:07 2021 +0200 +++ b/trytond/tests/test_field_many2many.py Sun Apr 25 17:56:39 2021 +0200 @@ -40,6 +40,21 @@ self.assertListEqual(many2manys, [many2many]) @with_transaction() + def test_search_equals_no_link(self): + "Test search many2many equals without link" + Many2Many = self.Many2Many() + many2many, no_link = Many2Many.create([{ + 'targets': [('create', [{'name': "Target"}])], + }, { + }]) + + many2manys = Many2Many.search([ + ('targets', '=', "Target"), + ]) + + self.assertListEqual(many2manys, [many2many]) + + @with_transaction() def test_search_non_equals(self): "Test search many2many non equals" Many2Many = self.Many2Many() @@ -86,6 +101,21 @@ self.assertListEqual(many2manys, [many2many1]) @with_transaction() + def test_search_non_equals_no_link(self): + "Test search many2many non equals without link" + Many2Many = self.Many2Many() + many2many, no_link = Many2Many.create([{ + 'targets': [('create', [{'name': "Target"}])], + }, { + }]) + + many2manys = Many2Many.search([ + ('targets', '!=', "Target"), + ]) + + self.assertListEqual(many2manys, [no_link]) + + @with_transaction() def test_search_in(self): "Test search many2many in" Many2Many = self.Many2Many() diff -r 57d3aec52a0e -r 506360117958 trytond/tests/test_field_one2many.py --- a/trytond/tests/test_field_one2many.py Sat Apr 24 19:27:07 2021 +0200 +++ b/trytond/tests/test_field_one2many.py Sun Apr 25 17:56:39 2021 +0200 @@ -102,6 +102,21 @@ self.assertListEqual(one2manys, [one2many]) @with_transaction() + def test_search_equals_no_link(self): + "Test search one2many equals without link" + One2Many = self.One2Many() + one2many, no_link = One2Many.create([{ + 'targets': [('create', [{'name': "Target"}])], + }, { + }]) + + one2manys = One2Many.search([ + ('targets', '=', "Target"), + ]) + + self.assertListEqual(one2manys, [one2many]) + + @with_transaction() def test_search_non_equals(self): "Test search one2many non equals" One2Many = self.One2Many() @@ -148,6 +163,21 @@ self.assertListEqual(one2manys, [one2many1]) @with_transaction() + def test_search_non_equals_no_link(self): + "Test search one2many non equals without link" + One2Many = self.One2Many() + one2many, no_link = One2Many.create([{ + 'targets': [('create', [{'name': "Target"}])], + }, { + }]) + + one2manys = One2Many.search([ + ('targets', '!=', "Target"), + ]) + + self.assertListEqual(one2manys, [no_link]) + + @with_transaction() def test_search_in(self): "Test search one2many in" One2Many = self.One2Many()