Mar
7
2016

Learn – How to Create a Model in Odoo 8 with Example

Greetings Everyone,

In this blog-post you will learn what is a model and how to create a model in an odoo module.

What is a Model in Odoo?

In Odoo, model is a class that maps to the data relation (table) and potentially bridge tables (e.g. for many to many relations). It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table.

The same class can have methods for the manipulations on the corresponding data.

Basic Points about Odoo Model:

  • Each model is a Python class that subclasses openerp.models.Model.
  • Each attribute of the model represents a database field.

Example of a Model :-

My goal is to create a table/model (from now on I will say Model) which stores information about a student.

Now brainstorm and think what all data you can collect, for eg – student name, gender, contact number, course, his qualifications, right? Why not collect his Hobbies too.

Now I’ll show you how to achieve this by creating an Odoo Model. I have created a Model containing all this data.

Odoo Module Code:-

Steps of Creating a Model in Odoo

  • Create a class which inherits from openerp.models.Model
  • Define all the fields. In the above code, I have Char field for name, email and contact. Date field for capturing Date of birth of a student. A Selection field for gender while a Binary field for storing the student’s image. A DateTime field for capturing the registration date.
  • Define all the relational fields if required. For example, here I have created a Many2many field for inputting hobbies while  a One2many field to keep track of his qualifications.
  • Write down computed/functional fields if necessary and create their method. Here, I have created a field ‘age’ whose value is calculated from the date of birth of a student using method calculate_age.

SnapShot of Eclipse IDE :-

model in odoo

Model in Odoo – Click on the image to enlarge

 

 

 

 

 

 

 

 

 

 

 

model in odoo

Model in Odoo – Click to enlarge

Hope you learnt how to create a Model in Odoo module, your reviews/comments/suggestions are most welcomed, please comment in the section below, and subscribe to the newsletter to receive timely future updates of this blog.

Click here to further learn about creating a Form View for above created model.

Thank You.

 

 

 

Share This!
Share On Twitter
Share On Linkdin
Contact us

Related Posts

About the Author: Burhan Vakharia

I, Burhan Vakharia, am a python developer, CS Lover living in Gandhinagar, Gujarat, India. I have a degree in Computer Science and Engineering from Babaria Institute of Technology. I am currently working as Odoo Developer at Serpent Consulting Services Pvt. Ltd., Gandhinagar. Apart form Odoo Framework, i have worked on C# .Net and Android Platforms.

2 Comments+ Add Comment

  • is it possible to create a model based on a view {not a table} ?

    • In Odoo, A model is actually is actually a Database table. And views are created based on the model. So first you need a model, then create its views(tree, form, search, etc). Hope this helps.

Leave a comment

Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Contact us
Hide Buttons