コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
308 ビュー

Hi Odoo Community,

I’m running Odoo on-premise (Docker, no Studio) and I’m trying to make a few product fields compulsory when creating a product, in the same way that the Name field works (red highlight + “invalid fields” message if empty).

Specifically, I want these fields to be required:

  • Internal Reference (default_code)
  • Company (company_id)
  • Cost (standard_price)

What I’ve tried so far:

  • Created a custom module that inherits product.template and sets these fields with required=True.
  • Restarted Odoo and upgraded the module with -u.​

But nothing changes:

  • The fields can still be left blank.
  • No red highlight appears.
  • Only Name behaves as expected, since it is required in the core model.

Questions:

  1. Is it possible to make existing fields behave exactly like name (red highlight + cannot save without value)?

Environment:

  • Odoo 18, running in Docker (on-premise)
  • PostgreSQL in separate container

Any guidance (or working examples) would be appreciated.

Thanks in advance!

アバター
破棄

"Created a custom module that inherits product.template and sets these fields with required=True." - you should share your code.

最善の回答

Hi,


Try any of the following methods:-

1-View inheritance — enforce the requirement on the UI/frontend side (red highlight).


In your custom module, add an XML view inheritance for the product form:


<odoo>

  <record id="view_product_template_form_inherit_required" model="ir.ui.view">

    <field name="name">product.template.form.inherit.required</field>

    <field name="model">product.template</field>

    <field name="inherit_id" ref="product.product_template_form_view"/>

    <field name="arch" type="xml">

      <field name="default_code" position="attributes">

        <attribute name="required">1</attribute>

      </field>

      <field name="company_id" position="attributes">

        <attribute name="required">1</attribute>

      </field>

      <field name="standard_price" position="attributes">

        <attribute name="required">1</attribute>

      </field>

    </field>

  </record>

</odoo>


2- Model constraint — enforce the requirement on the database/backend side.


from odoo import models, fields, api

from odoo.exceptions import ValidationError


class ProductTemplate(models.Model):

    _inherit = "product.template"


    default_code = fields.Char(required=True)

    company_id = fields.Many2one(required=True)

    standard_price = fields.Float(required=True)


    @api.constrains("default_code", "company_id", "standard_price")

    def _check_required_fields(self):

        for rec in self:

            if not rec.default_code or not rec.company_id or rec.standard_price == 0.0:

                raise ValidationError("Internal Reference, Company, and Cost must all be set.")



Module:-

* https://apps.odoo.com/apps/modules/18.0/mandatory_field_highlight


Hope it helps

アバター
破棄