From 1d6d1e26017ca46dca960f0b22be14083538c237 Mon Sep 17 00:00:00 2001 From: bit4bit Date: Sat, 26 Jun 2021 21:32:24 +0000 Subject: [PATCH] One2Many se puede iterar como lista FossilOrigin-Name: 1de9daae362feb6693b928c4a58c11423127eb1783fdf509f0ef3083b5563b24 --- facho/model/fields/one2many.py | 3 +++ tests/test_model.py | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/facho/model/fields/one2many.py b/facho/model/fields/one2many.py index 231874b..182a1b0 100644 --- a/facho/model/fields/one2many.py +++ b/facho/model/fields/one2many.py @@ -41,6 +41,9 @@ class _Relation(): def __len__(self): return len(self.relations) + def __iter__(self): + for relation in self.relations: + yield relation class One2Many(Field): def __init__(self, model, name=None, namespace=None, default=None): diff --git a/tests/test_model.py b/tests/test_model.py index 159b5d2..d588c14 100644 --- a/tests/test_model.py +++ b/tests/test_model.py @@ -442,3 +442,27 @@ def test_model_one2many_with_on_changes(): assert len(invoice.lines) == 2 assert '' == invoice.to_xml() + +def test_model_one2many_as_list(): + class Line(facho.model.Model): + __name__ = 'Line' + + quantity = fields.Attribute('quantity') + + class Invoice(facho.model.Model): + __name__ = 'Invoice' + + lines = fields.One2Many(Line) + + invoice = Invoice() + line = invoice.lines.create() + line.quantity = 3 + line = invoice.lines.create() + line.quantity = 5 + + lines = list(invoice.lines) + assert len(list(invoice.lines)) == 2 + + for line in lines: + assert isinstance(line, Line) + assert '' == invoice.to_xml()