SKILL.md
PS Custom Upgrade Utils Skill
Use the custom_util module for PS-specific Odoo data migrations. These helpers extend the standard upgrade-util library to handle common PS patterns and ensure technical debt (like Studio fields) is cleanly versioned.
Summary of Helpers
customutil.customrenamemodel(cr, oldname, new_name):
- Renames the model and ensures its state is set to 'base'. Required for custom models to be correctly handled by the Odoo registry.
customutil.customrenamefield(cr, model, oldname, new_name):
- Renames a field and ensures its state is set to 'base'. Use this for all x or xstudio_ fields.
customutil.transfercustomfields(cr, srcmodule, destmodule, fieldsto_transfer):
- Moves fields between modules. fieldstotransfer is a list of (model, field) or (model, oldfield, newfield) tuples.
customutil.customrenamemodule(cr, oldname, new_name):
- Safely renames a custom module, handling cases where a new module record might already exist.
customutil.updatecustomviews(cr, listfields):
- Batch updates multiple fields in ALL views. listfields is a list of (model, oldfield, new_field) tuples.
customutil.views.edit.editviews(cr, { xmlid: (Operations) }):
- Fine-grained view modification using specific operation objects: - operations.ReplaceValue(search, replace): Search/replace strings or regex. - operations.UpdateAttributes(xpath, attrs): Update or remove (if None) XML attributes. - operations.RemoveFields(names): Shorthand to remove one or more fields. - operations.RenameElements(old, new)**: Renames elements (and their labels) while keeping other attributes.
customutil.updaterelatedfield(cr, listfields):
- Updates related definitions on fields that point to renamed custom fields.
customutil.mergegroups(cr, srcxmlid, destxmlid):
- Merges one security group into another, remapping users and updating references.
customutil.mergemodelanddata(cr, sourcemodel, targetmodel, copyfields, setvalues=None):
- Merges one model into another, copying the records and remapping all IDs and references.
customutil.renamexmlids(cr, pairs, detect_module=True):
- Renames a batch of XML IDs. pairs is a list of (oldxmlid, newxmlid) tuples.
Usage Example
from odoo.upgrade import util
from custom_util import custom_util
def migrate(cr, version):
# Rename a custom model and field
custom_util.custom_rename_model(cr, 'x_my_old_model', 'my.new.model')
custom_util.custom_rename_field(cr, 'my.new.model', 'x_my_old_field', 'my_new_field')
# Transfer Studio fields
custom_util.transfer_custom_fields(cr, 'studio_customization', 'my_module', [
('res.partner', 'x_studio_custom_field', 'custom_field'),
])
# Bulk update views for renamed fields
custom_util.update_custom_views(cr, [
('res.partner', 'x_old_field', 'new_field'),
])
Best Practices
- Registry Health: Always use
custom_utilfor custom/studio fields to keep the Odoo registry in the standard
'base' state.
- Documentation: If a field is renamed, verify if other fields'
relatedordependsattributes need updating
via updaterelatedfield.