API Reference
rename_field

rename_field(class_obj, old_name, new_name) 🏷️

This method renames a field for a given class. It acts as a schema migration utility that modifies the internal field layout for all existing and future instances of that class. After execution, the field is accessible only by its new name.


Parameters

  • class_obj (type): The class object for which the field will be renamed. This must be a class managed by dbzero.
  • old_name (str): The current name of the field you want to change.
  • new_name (str): The new name for the field.

Returns

This method does not return any value. It modifies the class layout in place.


Side Effects

The rename_field method directly alters the class definition. Consequently, the old_name will no longer be a valid attribute on any instance of the class, and attempting to access it will result in an AttributeError.

💡

The operation is idempotent. If you call rename_field for a field that has already been renamed (i.e., the old_name no longer exists on the class layout), the method will not raise an error and will exit gracefully.


Example

# Assume DynamicDataClass is managed by dbzero and is initialized
# with fields like 'field_0', 'field_1', etc.
obj = DynamicDataClass(50)
 
# Initially, we can access 'field_33'
print(obj.field_33) # Outputs: 33
 
# Rename the field across all instances of DynamicDataClass
db0.rename_field(DynamicDataClass, "field_33", "renamed_field")
 
# The data is now accessible via the new name
print(obj.renamed_field) # Outputs: 33
 
# Accessing the old name will now raise an error
# obj.field_33 # -> raises AttributeError