跳至内容
菜单
此问题已终结
1 回复
962 查看

They have a chance to change the built in fields in adds-on module and change by your custom module like for example I want to delete the Job position field and replace it a new one.


形象
丢弃
最佳答案

you can follow these steps:

  1. Create a custom module: Create a new module that will contain your customizations. You can use the Odoo command-line tools or the Odoo Studio to create a new module scaffold.

  2. Inherit the target model: In your custom module, define a new Python class that inherits from the model that contains the field you want to modify. You can use the _inherit attribute to specify the target model to inherit from.

  3. Override the field: In the new class, redefine the field you want to modify with the desired changes. You can specify new attributes, such as string, required, readonly, etc., to customize the field behavior.

  4. Remove the original field: Use the _inherits attribute to specify the field you want to remove. Set the value to 'base.model_name', where model_name is the original model name containing the field you want to delete.

  5. Define the new field: Add the new field to your custom class with the desired attributes, such as string, required, readonly, etc.

  6. Update the views: If the original field is visible in any views, update those views to remove the reference to the original field and replace it with the new field.

Here's an example of how you can delete a built-in field (Job Position) and replace it with a new field (New Job Position) in a custom module:

from odoo import models, fields

class CustomModule(models.Model):
_inherit = 'base.model_name'

# Remove the original field
_inherits = {'base.model_name': 'original_field_id'}

# Define the new field
new_job_position = fields.Char(string="New Job Position")

# Override the original field(Replace original field)
original_field_id = fields.Many2one(comodel_name='base.model_name', string='Job Position', ondelete='cascade')

Make sure to replace 'base.model_name' with the actual model name that contains the field you want to modify.

Remember to update the views associated with the original model to remove the reference to the original field and include the new field in the desired views.

After creating and installing your custom module, the built-in field (Job Position) will be replaced with the new field (New Job Position) as defined in your custom module.

形象
丢弃