0% found this document useful (0 votes)
2 views11 pages

Models & Fields

This document outlines the structure and components of object-relational mapping in Odoo, detailing how models and fields are defined in Python. It explains the distinction between simple and relational fields, as well as various field attributes that can be set. Additionally, it includes exercises for creating a real estate properties model and adding fields with specific attributes.

Uploaded by

Joe Dalton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

Models & Fields

This document outlines the structure and components of object-relational mapping in Odoo, detailing how models and fields are defined in Python. It explains the distinction between simple and relational fields, as well as various field attributes that can be set. Additionally, it includes exercises for creating a real estate properties model and adding fields with specific attributes.

Uploaded by

Joe Dalton
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

PARTNERS

Chapter 2
Models & Fields
Object-Relational Mapping
SQL table
Python Class

Model
CLICK HERE
Models
1. The model is defined in the file
model.py module/models/model.py

from odoo import models


2. The file model.py is imported in
module/models/__init__.py
class TestModel(models.Model):
_name = "test.model"
_description = "Test Model"

3. The folder models is imported in


module/__init__.py

CLICK HERE
module/

├─ models/
│ ├─ __init__.py (2)
│ └─ model.py (1)

└─ __init__.py (3)
Fields

Define what the model can store and where it is stored.

from odoo import fields, models

Fields are defined as


class TestModel(models.Model):
attributes in the model. _name = "test.model"
_description = "Test Model"

field_1 = fields.Boolean()
field_2 = fields.Integer()
Fields

Two categories: Simple fields are atomic values stored


directly in the model's table

Relational fields are link between records


(of the same or different models).

CLICK HERE
Simple fields
Boolean - True / False

Char & Text - character string

Selection - list of string options

Float & Integer - numbers

Date & Datetime - dates

Binary - to store files, …

HTML - formatted text


Fields attributes

● String - field name displayed in views

● Invisible - True or False, displays the field or not in the views

● Readonly - True or False, if true then field cannot be edited by user

● Required - True or False, if true then record cannot be saved in database with an empty/null value

● Default - Sets value on the field automatically during record creation

● …

info = fields.Char(string="Test", required=True, default="test")


Exercise Define the real estate properties model.
PARTNERS

Chapter 2 1) In the estate module, add a new directory to store our models
python files.
New model
2) In this directory, create a new python file for the
estate_property table (don’t forget to add the __init__ too).

3) When the files are created, add a minimum definition for the
estate.property model.

4) Add a _description to your model to get rid of one of the


warnings.

Help: For now, just remember that in the future, we will try to have one model
per file in order to keep the project as object-oriented and clean as possible.
Exercise Add basic fields to the Real Estate Property table.
PARTNERS

Field Type
Chapter 2 name Char

New model description Text

Add fields postcode Char

date_availability Date

expected_price Float

selling_price Float

bedrooms Integer

living_area Integer

facades Integer
Exercise Add basic fields to the Real Estate Property table.
PARTNERS

Field Type
Chapter 2 garage Boolean

New model garden Boolean

Add fields garden_area Integer

Add fields (2) garden_orientation Selection

The garden_orientation field must have 4 possible values:


‘North’, ‘South’, ‘East’ and ‘West’.

The selection list is defined as a list of tuples


(see link below for example)

CLICK HERE
Exercise Set attributes for the following fields
PARTNERS

Chapter 2 Field Attribute

name required
New model
expected_price required
Add fields

Add fields (2)

Fields attributes

You might also like