Skip to content

Conversation

@hbrunn
Copy link
Member

@hbrunn hbrunn commented Nov 25, 2025

For a customer project, I'm migrating companies to operating units. This involves rewriting domains in ie ir.filters from [('company_id', '=', some_company_id)] to [('operating_unit_id', '=', the_corresponding_ou_id)].

Als I've encountered things like this several times already, I think it's time to have this in openupgradelib. We can make use of this function in merge_records and rename_fields eventually, but it's useful on its own for arbitrary domain fields.

Docstring:

Given a string containing a domain, this function
returns a text representation of it modified as executed by the
update_* functions passed.
:param domain: an Odoo domain like "[('some_field', '=', value)]"
:param update_name_func: function being passed a field name as string, returning a replacement string.
Example:
With

def rename_company_id(name):
    if name == 'company_id':
        return 'company_id2'
    return name

calling

update_domain("[('company_id', '=', 42), ('other_field', '=', value)]", update_name_func=rename_company_id)

returns

"[('company_id2', '=', 42), ('other_field', '=', value)]"

:param update_value_func: function being passed a domain field name as string, and a value as string, int, list or ast.Expression if the right hand side of a domain leaf cannot be parsed as one of the former. Return one of the aformentioned types to replace the right hand side of the domain leaf.
Example:
With

def change_company_id(name, value):
    if name == 'company_id':
        if isinstance(value, int) and value == 42:
            return 43
        elif isinstance(value, (list, tuple)) and 42 in value:
            return [val if val != 42 else 43 for val in value]
    return value

calling

update_domain("[('company_id', '=', 42), ('company_id', 'in', [41, 42]), ('other_field', '=', value)]", update_value_func=change_company_id)

returns

"[('company_id', '=', 43), ('company_id', 'in', (41, 43)), ('other_field', '=', value)]"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant