Field = cls._fields[fname] KeyError: during the slicing of a string

Hi,
I would like to do some customization, I want to do a search on a number during the import of a file starting from the position number 4 of the number ( ('bank_account.numbers.number'[4:], '=', number),, this is the code of the function that i would use for the search :

    @classmethod
    def get_by_bank_account(cls, company, number):
        journals = cls.search([
                ('company', '=', company),
                ['OR',
                    ('bank_account.numbers.number'[4:], '=', number),
                    ('bank_account.numbers.number_compact', '=', number),
                    ],
                ])
        if journals:
            journal, = journals
            return journal

This is the Traceback error :

File "/lib/python3.7/site-packages/trytond/modules/account_statement/journal.py", line 112, in get_by_bank_account
    ('bank_account.numbers.number_compact', '=', number),
  File "/lib/python3.7/site-packages/trytond/model/modelsql.py", line 1284, in search
    tables, expression = cls.search_domain(domain)
  File "/lib/python3.7/site-packages/trytond/model/modelsql.py", line 1471, in search_domain
    expression = convert(domain)
  File "/lib/python3.7/site-packages/trytond/model/modelsql.py", line 1468, in convert
    domain[1:] if domain[0] == 'AND' else domain)))
  File "/lib/python3.7/site-packages/trytond/model/modelsql.py", line 1467, in <genexpr>
    return And((convert(d) for d in (
  File "/lib/python3.7/site-packages/trytond/model/modelsql.py", line 1465, in convert
    return Or((convert(d) for d in domain[1:]))
  File "/lib/python3.7/site-packages/trytond/model/modelsql.py", line 1465, in <genexpr>
    return Or((convert(d) for d in domain[1:]))
  File "/lib/python3.7/site-packages/trytond/model/modelsql.py", line 1457, in convert
    field = cls._fields[fname]
KeyError: '_account'

Hi,

unfortunately this is not a valid domain clause in Tryton. Slicing is not supported.
But you could try:

                    ('bank_account.numbers.number', 'ilike', '%{}'.format(number)),
1 Like

It could be even more precise as:

                    ('bank_account.numbers.number', 'like', '____' + number)

_ is the SQL wildcard for any one char (and % is the wildcard for any number of char)

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.