Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-theme.php on line 544

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp-block-list.php on line 199

Deprecated: Return type of WPCF7_FormTag::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/form-tag.php on line 396

Deprecated: Return type of WPCF7_FormTag::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/form-tag.php on line 388

Deprecated: Return type of WPCF7_FormTag::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/form-tag.php on line 382

Deprecated: Return type of WPCF7_FormTag::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/form-tag.php on line 400

Deprecated: Return type of WPCF7_Validation::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/validation.php on line 78

Deprecated: Return type of WPCF7_Validation::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/validation.php on line 72

Deprecated: Return type of WPCF7_Validation::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/validation.php on line 59

Deprecated: Return type of WPCF7_Validation::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-content/plugins/contact-form-7/includes/validation.php on line 82

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home/odootech/domains/odootechnical.com/public_html/wp-content/themes/newsup/inc/ansar/newsup-custom-navwalker.php on line 41

Deprecated: DateTime::__construct(): Passing null to parameter #1 ($datetime) of type string is deprecated in /home/odootech/domains/odootechnical.com/public_html/wp-includes/script-loader.php on line 348

Deprecated: Return type of Requests_Cookie_Jar::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Cookie/Jar.php on line 63

Deprecated: Return type of Requests_Cookie_Jar::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Cookie/Jar.php on line 73

Deprecated: Return type of Requests_Cookie_Jar::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Cookie/Jar.php on line 89

Deprecated: Return type of Requests_Cookie_Jar::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Cookie/Jar.php on line 102

Deprecated: Return type of Requests_Cookie_Jar::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Cookie/Jar.php on line 111

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 40

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetGet($key) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 51

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetSet($key, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 68

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::offsetUnset($key) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 82

Deprecated: Return type of Requests_Utility_CaseInsensitiveDictionary::getIterator() should either be compatible with IteratorAggregate::getIterator(): Traversable, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/odootech/domains/odootechnical.com/public_html/wp-includes/Requests/Utility/CaseInsensitiveDictionary.php on line 91

Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/odootech/domains/odootechnical.com/public_html/wp-includes/class-wp.php on line 173
Learn – How to Create a Model in Odoo 8 with Example – Odoo Technical

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.


Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/odootech/domains/odootechnical.com/public_html/wp-includes/wp-db.php on line 3030

Deprecated: ltrim(): Passing null to parameter #1 ($string) of type string is deprecated in /home/odootech/domains/odootechnical.com/public_html/wp-includes/wp-db.php on line 3030
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

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