Hello Odooers & Developers,
Introduction
Welcome to our Odoo Technical Article! In this article, we will explore how to create a Many2one field in Odoo to establish relationships between models effectively. The Many2one field is widely used to link records from one model to a single record of another model, making data management more structured and efficient.
In this article, you'll learn:
- What is a Many2one field, and when should you use it?
- How to define a Many2one field in Python.
- A practical example of implementing a Many2one field in Odoo.
What is a Many2one Field in Odoo?
A Many2one field in Odoo is a relational field that defines a "many-to-one" relationship between models. This means multiple records from one model can be linked to a single record of another model. This field is essential for maintaining structured data and navigation between related records.
Features of Many2one Field:
Creates a "many-to-one" correspondence between models.
Facilitates simple access to corresponding model records.
- It has support for several parameters such as string, required, domain, delete, context, help, etc.
- It provides a dropdown selection in the user interface for choosing related records.
How to Define a Many2one Field in Odoo
To define a Many2one field in Odoo, use the fields Many2one inside your model.
Below is the syntax for defining a Many2one field in the py file of models.
Syntax:
.py
from odoo import models, fields
class ModelName(models.Model):
_name = 'model.name'
field_name = fields.Many2one('related.model', string='Field Label' )
Explanation of Parameters:
- 'related.model': The name of the related model in string format (e.g., 'res.partner' for linking to partners).
- string: The label that appears in the UI for the field.
- required (optional): Set to True if the field is mandatory.
- ondelete (optional): Determines what happens when a related record is deleted (options: cascade, restrict, set null).
- domain (optional): Restricts the available records based on a condition.
- context (optional): Passes additional data when selecting related records.
Practical Example: Implementing a Many2one Field
Let's implement a Many2one field that links a Shop model to a Mall model.
So below is the code of py file of Shop model and Mall model -
.py
from odoo import models, fields
class Mall(models.Model):
_name = 'mall.mall'
_description = 'Mall'
name = fields.Char(string='Mall Name', required=True)
class Shop(models.Model):
_name = 'shop.shop'
_description = 'Shop'
name = fields.Char(string='Shop Name', required=True)
mall_id = fields.Many2one('mall.mall', string='Mall', required=True, ondelete='cascade')
Explanation:
- The Mall model contains a name field to store the mall's name.
- The Shop model has a Many2one field mall_id, linking each shop to a single mall.
- The ondelete='cascade' ensures that if a mall is deleted, all associated shops will be deleted automatically.
Adding Many2one Field in XML View
To display the Many2one field in the form view, add the following XML code:
.py
<record id="view_shop_form" model="ir.ui.view">
<field name="name">shop.shop.form</field>
<field name="model">shop.shop</field>
<field name="arch" type="xml">
<form string="Shop Form">
<sheet>
<group>
<field name="name"/>
<field name="mall_id"/>
</group>
</sheet>
</form>
</field>
</record>
Explanation:
- The <field name="mall_id"/> tag adds the Many2one field in the form view, allowing users to select a mall from a dropdown.
Conclusion
The Many2one field is an essential feature in Odoo that helps structure relationships between models efficiently. By using Many2one fields, you can enhance data organization and simplify navigation within your application.
If you want odoo technical training on any odoo version , please let us know by mail at [email protected]. Then next, our odoo expert will conduct online or offline Odoo training with you
Devintellecs & team are odoo training providers in the USA and INDIA, so we will try our best to give the training either individal or any bulk employee company.
If you want to check your odoo technical or functional knowledge, then we have prepared the odoo EXam practice test for the odoo technical & functional people.