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:-

from openerp import models, fields, api


class student_student(models.Model):
    _name = 'student.student'

    @api.depends('dob')
    def calculate_age(self):
        """ Description:- This method calculates the age on the basis of the 
        Birth Date entered in the 'dob' field. """
        for data in self:
            if data.dob:
                current_year = datetime.datetime.now().year
                birth_year = datetime.datetime.strptime(data.dob,"%Y-%m-%d").year
                age = current_year - birth_year
                data.age = age

    #Basic Fields
    name = fields.Char('Student Name')
    email = fields.Char('Email')
    contact = fields.Char('Contact Number')
    dob = fields.Date('Date of Birth')
    registration_date = fields.Datetime('Registration Date')
    is_physically_disabled = fields.Boolean('Is Physically Disabled?')
    image = fields.Binary('Image')
    gender = fields.Selection([('male','Male'),('female','Female')], string='Gender')
    #Relational Fields
    course_id = fields.Many2one('course.course', string='Course')
    hobby_ids = fields.Many2many('hobby.hobby', 'student_hobbies_rel', 'student_id', 'hobby_id', string='Hobby')
    qualification_ids = fields.One2many('qualification.qualification', 'student_id', \
                                        string='Qualification')
    #Computed Field
    age = fields.Float(compute=calculate_age ,string='Age')


class qualification_qualification(models.Model):
    _name = 'qualification.qualification'

    student_id = fields.Many2one('student.student', string='Student')
    course_id = fields.Many2one('course.course', string='Course')
    year_cleared = fields.Date('Completed On')
    grade = fields.Float('Grade(%)')


class course_course(models.Model):
    _name = 'course.course'

    name = fields.Char('Course Name')
    
class hobby_hobby(models.Model):
    _name = 'hobby.hobby'

    name = fields.Char('Hobby Name')

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.

 

 

 

By Burhan Vakharia

Having 6+ years of experience working on Odoo with projects covering several business verticles like Retail industry, Manufacturing, Service, Rental, Education, Medical and many more. I am having experience working on Odoo projects from several countries across the globe.

2 thoughts on “Learn – How to Create a Model in Odoo 8 with Example”
    1. 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 Reply to ravi Cancel reply

Your email address will not be published. Required fields are marked *