Enable Dark Mode!
a-complete-overview-of-field-parameters-in-odoo-19.jpg
By: Milton Tom

A Complete Overview of Field Parameters in Odoo 19

Technical Odoo 19 Odoo Community Odoo Enterprises

Fields are more than just database columns; they represent small objects that take care of data validation, presentation, searching, computation, and security. Everything about them can be configured using field parameters.

Common Parameters

  • string - The label shown to users. If you don't set it, Odoo capitalizes the field's Python name.
  • help - The tooltip text shown when a user hovers over the field's label in the UI.
  • required - Marks the field as mandatory. A record can't be created or saved without a value, unless a default is given.
  • readonly - Prevents editing from the UI.
  • index - Controls how the field is indexed at the database level. This only affects stored fields.
  • groups - A comma-separated list of security group XML IDs. Only users in one of those groups can see or set the field.
  • company_dependent - Makes the field's value vary per company, stored internally as a JSON dict keyed by company ID rather than a plain column.
  • store - Whether the field is persisted in the database.
name = fields.Char(string="Customer Reference")
note = fields.Char(help="Internal reference shown on the invoice")
name = fields.Char(required=True)
code = fields.Char(readonly=True)
ref = fields.Char(index=True)  # or index='btree_not_null' / 'trigram'
cost = fields.Float(groups="base.group_system")
price = fields.Float(company_dependent=True)
total = fields.Float(compute="_compute_total", store=True)

Aggregation Parameters

  • aggregator - The default aggregate function applied when grouping. Supported values: count, count_distinct, bool_and, bool_or, max, min, avg, and sum.
  • group_expand - A method name used to control which groups appear (and in what order) when grouping by this field, even for groups with zero records
amount = fields.Float(aggregator="sum")  # count, count_distinct, bool_and, bool_or, max, min, avg, sum
stage_id = fields.Many2one("my.stage", group_expand="_read_group_stage_ids")

Field Type Specific Parameters

Char / Text

  • size - Maximum character length. Only available on Char, since Text fields are meant for longer, unbounded content.
  • trim - Whether leading and trailing whitespace is stripped before saving (default True). Keeps form input and imported data consistent.
  • translate - Enables translation of the field's value; can be set to True or to a callable for more advanced translation handling.
code = fields.Char(size=10)
name = fields.Char(trim=False)
title = fields.Char(translate=True)

Float

  • digits - A (total, decimal) tuple, or the name of a decimal.precision record, controlling rounding precision.
  • min_display_digits - The minimum number of decimals shown in the UI, independent of what's actually stored, useful when you want a value like 3.1 displayed as 3.100.
price = fields.Float(digits=(16, 2))  # or digits="Product Price"
rate = fields.Float(min_display_digits=3)

Binary

  • attachment - Whether the value is stored as an ir.attachment or directly as a column in the model's table.
file = fields.Binary(attachment=False)

Image (extends Binary)

  • max_width / max_height - Resize limits in pixels, with aspect ratio preserved. A value of 0 means no limit.
  • verify_resolution - Whether Odoo checks that the image doesn't exceed the maximum allowed resolution before accepting it.
image = fields.Image(max_width=1024, max_height=1024)
photo = fields.Image(verify_resolution=True)

Html

  • sanitise – Whether the value should be sanitized.
  • sanitize_overridable – Allows users in the Sanitize-Override security group to avoid sanitization altogether
  • sanitize_tags – Tags are restricted to a whitelist.
  • sanitize_attributes – Keeps attributes on a secure whitelist.
  • sanitize_style – Style attribute sanitization flag.
  • sanitize_conditional_comments – Whether conditional comments are stripped.
  • sanitize_output_method – Whether output is sanitized as html or xhtml.
  • strip_style - Whether style attributes are removed outright rather than sanitized.
  • strip_classes - Whether class attributes are removed outright.
body = fields.Html(sanitize=False)
body = fields.Html(sanitize_overridable=True)
body = fields.Html(sanitize_tags=False)
body = fields.Html(sanitize_attributes=False)
body = fields.Html(sanitize_style=True)
body = fields.Html(sanitize_conditional_comments=False)
body = fields.Html(sanitize_output_method="html")
body = fields.Html(strip_style=True)
body = fields.Html(strip_classes=True)

Monetary

  • currency_field - Name of the Many2one field that determines the currency symbol and decimal precision used to display the value.
amount = fields.Monetary(currency_field="currency_id")

Selection

  • selection - A list of (value, label) pairs, a method, or a method name that returns the available options.
  • selection_add - Extends an inherited selection field with new options, inserted in a defined order relative to the existing list.
  • ondelete - A fallback action per selection_add option ('set null', 'cascade', 'set default', 'set VALUE', or a callable), applied to existing records if that option is ever removed.
state = fields.Selection([("draft", "Draft"), ("done", "Done")], default="draft")
state = fields.Selection(selection="_get_states")  # method name
state = fields.Selection(selection_add=[("review", "In Review")])  # in an inherited model
state = fields.Selection(selection_add=[("review", "In Review")], ondelete={"review": "set default"})

Relational Field Parameters

Many2one

  • comodel_name - The target model. Mandatory unless the field is related or extends an existing field.
  • domain - Restricts which records are selectable and evaluated client-side.
  • context - Extra context is passed when the client interacts with the field.
  • ondelete - What happens on deletion of the linked record: 'set null', 'restrict', or 'cascade'.
  • bypass_search_access - Skips access-right checks on the comodel when searching for candidate records.
  • delegate - Enables _inherits-style delegation, exposing the target model's fields directly on the current model.
  • check_company - Enforces multi-company consistency between the record and the record it links to.
partner_id = fields.Many2one(comodel_name="res.partner")
partner_id = fields.Many2one("res.partner", domain=[("is_company", "=", True)])
partner_id = fields.Many2one("res.partner", context={"default_is_company": True})
partner_id = fields.Many2one("res.partner", ondelete="restrict")  # 'set null', 'restrict', 'cascade'
partner_id = fields.Many2one("res.partner", bypass_search_access=True)
partner_id = fields.Many2one("res.partner", delegate=True)
partner_id = fields.Many2one("res.partner", check_company=True)

One2many

  • comodel_name and inverse_name – Name of the target model and the Many2one field of this target model referencing the current record. Both are compulsory, except for related fields.
  • domain, context, bypass_search_access – Same meaning as on Many2one.
line_ids = fields.One2many(comodel_name="my.line", inverse_name="order_id")
line_ids = fields.One2many("my.line", "order_id", domain=[("active", "=", True)])
line_ids = fields.One2many("my.line", "order_id", context={"default_qty": 1})
line_ids = fields.One2many("my.line", "order_id", bypass_search_access=True)

Many2many

  • comodel_name - The target model, mandatory except for related or extended fields.
  • relation, column1, column2 - The name of the join table and its two foreign-key columns.
  • domain, context, check_company - Same meaning as on Many2one.
tag_ids = fields.Many2many(comodel_name="my.tag")
tag_ids = fields.Many2many("my.tag", relation="my_model_tag_rel", column1="model_id", column2="tag_id")
tag_ids = fields.Many2many("my.tag", domain=[("active", "=", True)])
tag_ids = fields.Many2many("my.tag", context={"default_color": 1})
tag_ids = fields.Many2many("my.tag", check_company=True)

Computed Field Parameters

  • compute – The method that assigns the field's value.
  • compute_sudo – Runs the compute method as a superuser, bypassing access rights.
  • precompute – True – In that case, the field will be calculated before inserting the record into the database and not after it has been inserted. Best for computing the fields of newly created records cheaply in bulk. Supplying a value or a default will disable precomputing regardless of setting precompute=True.
  • recursive – Must be specified explicitly when there is an indirect dependency of a computed field on itself in a sequence, such as parent_id.total.
  • inverse – A technique that would enable users to write into a read-only computed column by reversing its calculation.
  • related – A field path made up of dots where a field just reflects the value of another field in a related record, without the need for any compute function at all.
  • search – Enables a non-persisted (and possibly persisted) field to be searched, by providing a method that will take an operator and value and return the real world value. The method should indicate that it does not support the operator so that it can try another operator.
total = fields.Float(compute="_compute_total")
total = fields.Float(compute="_compute_total", compute_sudo=True)
total = fields.Float(compute="_compute_total", store=True, precompute=True)
total = fields.Float(compute="_compute_total", store=True, recursive=True)  
total = fields.Float(compute="_compute_total", inverse="_inverse_total")
partner_name = fields.Char(related="partner_id.name")
total = fields.Float(compute="_compute_total", search="_search_total")

Moreover, several fields can utilize the same compute method by just referring to the same method name and listing the same dependencies, and the method will assign the value of each field one after another.

Field parameters are the small knobs Odoo allows us to adjust to configure the way our data is saved, protected, viewed, and searched without having to code a single view XML statement. The secret of how to make use of them lies not in the knowledge of all available options but in understanding the category of a parameter.

To read more about What are the Key Field Parameters in Odoo 18, refer to our blog What are the Key Field Parameters in Odoo 18.


Frequently Asked Questions

What is the purpose of the digits attribute in a Float field?

It defines the round-off precision in terms of a (total, decimal) tuple, like digits=(16, 2) or as a reference to a decimal.precision entry, like digits="Product Price".

How do I prevent Odoo from storing a Binary field as an attachment?

The attribute attachment=False will store the value directly as a database field in the table for the model, not as an ir.attachment.

What is the role of selection_add and ondelete in a Selection field?

The attribute selection_add adds additional entries to a selection field, whereas ondelete assigns an alternative action to each one, like ‘set default’ or ‘cascade’, if the selected entry is deleted.

If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



WhatsApp