In Odoo 17, a model is a class that represents a specific data relationship or table. It encapsulates all the essential fields and functionalities required for storing and managing the associated data. Each model typically corresponds to a distinct database table.
Key Components of a Model
_name: This attribute defines the name of the model in the Odoo system. It is expressed in dot-notation within the module namespace
_name = 'module.model_name'
Fields: Fields are declared as attributes within the model. They define the columns in the corresponding database table
from odoo import fields
class ExampleModel(models.Model):
_name = 'example.model'
name = fields.Char(string='Name')
description = fields.Text(string='Description')
Methods: Methods define the behaviors and functionalities of the model. They can be used to manipulate data, validate inputs, and perform various operations.
def create(self, vals):
if not vals.get('name'):
raise ValidationError('Name is required')
return super(ExampleModel, self).create(vals)
To create a model in Odoo 17, you need to define a Python class that extends models.Model. Here’s a simple example:
from odoo import models
class ExampleModel(models.Model):
_name = 'example.model'
name = fields.Char(string='Name')
description = fields.Text(string='Description'
You can add various fields and methods to your model to define its structure and behavior. For example:
class ExampleModel(models.Model):
_name = 'example.model'
name = fields.Char(string='Name')
description = fields.Text(string='Description')
@api.model def create(self, vals):
if not vals.get('name'):
raise ValidationError('Name is required')
return super(ExampleModel, self).create(vals)
Models in Odoo 17 are fundamental building blocks that define the logical structure of your database and how data is stored, organized, and manipulated. By understanding and utilizing models effectively, you can create powerful and flexible applications in Odoo.