# Iterate with pyson?

**URL:** https://discuss.tryton.org/t/iterate-with-pyson/1990
**Category:** Developer
**Created:** [November 23, 2019, 1:04pm UTC](https://discuss.tryton.org/t/iterate-with-pyson/1990 "2019-11-23T13:04:42Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![risto3](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@risto3](https://discuss.tryton.org/u/risto3)
#### Post date: [November 23, 2019, 1:04pm UTC](https://discuss.tryton.org/t/iterate-with-pyson/1990/1 "2019-11-23T13:04:42Z")

</div>

I’m trying to add a visual to account.move to find unbalanced draft moves.  
Is it possible to iterate with pyson?  
This gives me an error _TypeError: ‘Eval’ object is not iterable_

```
@classmethod
def view_attributes(cls):
    return [
        ('/tree', 'visual',
         If(((Eval('state') == 'draft')
            & sum(l.debit - l.credit for l in Eval('lines')))),
            'warning', ''),
        ]

```

Any hint on how to maybe do this?

---

<div class="post-metadata">

### Author: ![pokoli](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/pokoli/32/22_2.png) [@pokoli](https://discuss.tryton.org/u/pokoli)
#### Post date: [November 23, 2019, 5:01pm UTC](https://discuss.tryton.org/t/iterate-with-pyson/1990/2 "2019-11-23T17:01:55Z")

</div>

This is not possible, you should create a function field that return the sum of credit and debit of your lines.

---

<div class="post-metadata">

### Author: ![risto3](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@risto3](https://discuss.tryton.org/u/risto3)
#### Post date: [November 23, 2019, 6:53pm UTC](https://discuss.tryton.org/t/iterate-with-pyson/1990/3 "2019-11-23T18:53:19Z")

</div>

Ok, I suspected as such.

So I added

```
balanced = fields.Function(fields.Boolean('Balanced'), 'get_balanced')

def get_balanced(self, name):
    'return balance state of move lines'
    if self.lines is not None:
        return sum(line.debit - line.credit for line in self.lines) == 0
    else:
        return False # empty move unbalanced?

```

which I can confirm works okay via trytond-console

But the following still does not colour the unbalanced moves in the GTK client moves tree

```
@classmethod
def view_attributes(cls):
    return [
        ('/tree', 'visual',
            If(((Eval('state') == 'draft') & ~Bool(Eval('balanced'))),
               'danger', '')),
        ]

```

Is there something else I’m missing?

[edited: strange, after restarting server/client, now I get **all** lines in red even though  
balanced is False for only one move in my base when running trytond-console… I added a  
dependency to ‘lines’ but that doesn’t seem to change anything]

---

<div class="post-metadata">

### Author: ![risto3](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@risto3](https://discuss.tryton.org/u/risto3)
#### Post date: [November 24, 2019, 8:11am UTC](https://discuss.tryton.org/t/iterate-with-pyson/1990/4 "2019-11-24T08:11:58Z")

</div>

Perhaps there are problems with the pyson evaluation of the fields.Function balanced…  
Adding a default value of `True`, the tree is no longer all red.

```auto
@classmethod
def view_attributes(cls):
    return [
        ('/tree', 'visual',
            If(((Eval('state') == 'draft') & ~Bool(Eval('balanced',True))),
               'danger', '')),
        ]

```

from the console I compare ‘state’ with ‘balanced’:

```auto
>>> i.fields_get('balanced')
{'balanced': {'context': '{}', 'loading': 'lazy', 'name': 'balanced', 'on_change': [], 'on_change_with': [], 'readonly': True, 'required': False, 'states': '{}', 'type': 'boolean', 'domain': '[]', 'searchable': False, 'sortable': False, 'string': 'Balanced', 'help': '', 'create': True, 'delete': True}}
>>> i.fields_get('state')
{'state': {'context': '{}', 'loading': 'eager', 'name': 'state', 'on_change': [], 'on_change_with': [], 'readonly': True, 'required': True, 'states': '{}', 'type': 'selection', 'domain': '[]', 'searchable': True, 'sortable': True, 'string': 'État', 'help': '', 'selection': [('draft', 'Brouillon'), ('posted', 'Posté')], 'selection_change_with': [], 'sort': True, 'create': True, 'delete': True}}

```

Seemingly the only pertinent differences is that ‘balanced’ has _lazy_ ‘loading’ and no searcher.

---

<div class="post-metadata">

### Author: ![ced](https://discuss-cdn.tryton.org/user_avatar/discuss.tryton.org/ced/32/1237_2.png) [@ced](https://discuss.tryton.org/u/ced)
#### Post date: [November 24, 2019, 11:33am UTC](https://discuss.tryton.org/t/iterate-with-pyson/1990/5 "2019-11-24T11:33:23Z")

</div>

The fields used in `view_attributes` `PYSON` expression must be present in the view to be actually evaluated otherwise it is the default value that is used.  
We could have some helper to ensure to add some fields with the view\_attributes: [Add depends fields on view\_attributes (#8862) · Issues · Tryton / Tryton · GitLab](https://bugs.tryton.org/issue8862)

---

<div class="post-metadata">

### Author: ![risto3](https://discuss-cdn.tryton.org/letter_avatar_proxy/v4/letter/r/a87d85/32.png) [@risto3](https://discuss.tryton.org/u/risto3)
#### Post date: [November 30, 2019, 8:10am UTC](https://discuss.tryton.org/t/iterate-with-pyson/1990/6 "2019-11-30T08:10:57Z")

</div>

okay, in the mean time, I added `<field name="balanced" tree_invisible="1"/>` to move\_tree.xml and now I see the ‘danger’ highlighting hoped for.
