Skip to Content
Menu
Dette spørgsmål er blevet anmeldt
2 Besvarelser
171 Visninger

Hi

My users need some guidelines when creating products, like pre-populated fields or fields and attributes to not forget. For "normal" fields, it is easily done with default values or server actions or external modules. But I can't find how to automaticaly add attributes fields (without values) to product form. Any clue ?

I'm using Odoo 16


Avatar
Kassér
Bedste svar

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

Avatar
Kassér
Forfatter

Thanks a lot! it is exactly what I need : both solutions are good depending on users need.

Bedste svar

Hello Xavier, I understand you're looking to pre-populate product forms with attributes in Odoo 16 to guide your users.



  Unfortunately, Odoo doesn't natively support adding attributes to a product form without values directly through default values or server actions.

  However, you can achieve a similar result by creating a product template with the desired attributes (without values) and then duplicating this template each time a new product needs to be created. This ensures the attributes are present.

  Another approach is to develop a custom module that overrides the product creation form and adds the desired attributes programmatically. This would require some Python coding and XML view customization.

  You could also explore using Odoo Studio if you have it, as it might offer some low-code customization options to achieve this, although it might still require some coding for complex scenarios.


For personalized assistance:
https://www.pragtech.co.in/contact-us-mql.html

Avatar
Kassér