In this blog-post you will learn what is a model and how to create a model in an odoo module.
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.
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.
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')
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.
]]>