One2Many se puede iterar como lista
FossilOrigin-Name: 1de9daae362feb6693b928c4a58c11423127eb1783fdf509f0ef3083b5563b24
This commit is contained in:
		| @@ -41,6 +41,9 @@ class _Relation(): | |||||||
|     def __len__(self): |     def __len__(self): | ||||||
|         return len(self.relations) |         return len(self.relations) | ||||||
|  |  | ||||||
|  |     def __iter__(self): | ||||||
|  |         for relation in self.relations: | ||||||
|  |             yield relation | ||||||
|  |  | ||||||
| class One2Many(Field): | class One2Many(Field): | ||||||
|     def __init__(self, model, name=None, namespace=None, default=None): |     def __init__(self, model, name=None, namespace=None, default=None): | ||||||
|   | |||||||
| @@ -442,3 +442,27 @@ def test_model_one2many_with_on_changes(): | |||||||
|  |  | ||||||
|     assert len(invoice.lines) == 2 |     assert len(invoice.lines) == 2 | ||||||
|     assert '<Invoice count="2"><Line quantity="3"/><Line quantity="5"/></Invoice>' == invoice.to_xml() |     assert '<Invoice count="2"><Line quantity="3"/><Line quantity="5"/></Invoice>' == 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><Line quantity="3"/><Line quantity="5"/></Invoice>' == invoice.to_xml() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user