Hi,
To automatically add product attributes without values in Odoo 16, you need specific methods since standard defaults don't handle these relational fields. The most straightforward approach is to create a default product template pre-configured with all necessary attributes, which users can duplicate for new products. This method is user-friendly and requires no coding, leveraging Odoo's built-in functionality effectively.
Steps:-
1-Create the Template: Go to Products → Products, create a new product, and name it something like "_DEFAULT_PRODUCT_TEMPLATE" or "Product Base".
2-Configure Attributes: On this template, add all the common attributes you want to appear on new products (e.g., Size, Color, Material). Leave the attribute values empty or set defaults as needed.
3-Use "Duplicate" Functionality: Instruct your users to simply duplicate this base template whenever they need to create a new product. The new product will inherit all the attributes from the copied template.
For a more automated solution, you can use a server action triggered on product creation. This involves writing Python code to programmatically add predefined attribute IDs to new product records. Alternatively, developers can implement the most robust approach by creating a custom module that extends the product template's create method, allowing for complex logic based on categories or other fields.
The recommended path for most businesses is to start with the default template method due to its simplicity. If greater automation and enforcement are required, you can then progress to the technical solutions involving server actions or custom module development, which require more maintenance but offer greater control.
For the most control, you can create a custom module that extends the create method of
from odoo import models, fields, api
class ProductTemplate(models.Model):
_inherit = 'product.template'
@api.model
def create(self, vals):
# First, create the product template
product = super(ProductTemplate, self).create(vals)
# Define the attributes to add. You can make this logic as complex as needed.
# For example, base it on the product's category (vals.get('categ_id'))
attribute_ids = [1, 2, 3]
for attribute_id in attribute_ids:
# Use the same logic as in the server action
if not product.attribute_line_ids.filtered(lambda l: l.attribute_id.id == attribute_id):
product.write({
'attribute_line_ids': [(0, 0, {
'attribute_id': attribute_id,
'value_ids': [(6, 0, [])]
})]
})
return product
Hope it helps