Enable Dark Mode!
By: Gayathri V

ORM Methods in Odoo 15

Technical Odoo 15

Odoo is an open-source ERP simple and extensible software that has been popular in the big and small industries since its first release. ORM methods are also one of the powerful features that help you execute SQL queries without explicitly writing them out. ORM methods allow users to implement the concept of OOPS and interact with the database. ORM fills the gap between the database and OOP classes and objects.
The ORM makes it easy to manage the creation of tables and fields based on the class definitions you provide to your model. By definition, Odoo creates and manages tables in the database. This reduces the work of creating tables using queries. It also helps developers focus on the business logic of their application, as it requires less code to develop the CRUD portion of the application and puts a strain on the application.
The following are some of the common ORM methods.
Here are some common ORM methods for the Odoo:
1. create/update
Create() method is used to create new records in a model.vals_list is the dictionary used to initialize new records using the value.
Eg: customer = self.env['res.partner'].create({    'name': 'Lead1 Email Customer'})
Model.copy(default = None)
Copy() method is used to duplicate the record, and it is updated with the default values.
Eg: lead_dup = lead.copy({'name': 'Duplicate'})
Which is used to get the default values for the fields_list,
Default values are context, defaults of the user, and the model itself.
Eg: @api.model
def default_get(self, fields):
    result = super(CrmLeadConvert2Ticket, self).default_get(fields)
return result
To create the new record call create () with only one value, the display name of the new record.
New records are initialized with default values that apply to this model or are provided via context. The normal create () behavior is applied.
Eg: self.env['res.partner'].name_create('Agrolait')
Which is used to update all records in the current set with the specified values.
Eg: self.write({'state': 'open'})
Model.flush(fnames=None, records=None)
The method is usually used to handle pending calculations (on all models) and removes pending updates to the database.
Eg: self.env['account.move.line'].flush(['account_id', 'journal_id'])
2. Search/Read
Returns a record set of IDs provided as parameters in the current environment.
Eg: for employees in self
employee.employee_id = self.env['hr.employee'].browse(employee.id)
Model.search(args[, offset=0][, limit=None][, order=None][, count=False])
Which searches for the records in the args domain.
Eg:  self.env['crm.lead'].search([
             ('phone_mobile_search', 'like', '0499223311'))]
Which returns the number of records in the current model that matches the specified domain.
Eg: user_connected =   self.env['res.users'].search_count([('company_id', '=', company.id), ('login_date', '>=', start), ('login_date', '<', end)])
Model.name_search(name='', args=None, operator='ilike', limit=100)
It finds records with a display name that matches the specified name pattern when compared to the specified operator and also matches the optional search domain (argument).
This method is the same as calling search () on the search domain based on display_name and then name_get () based on the search results.
Eg: doc_name_get= self.env[values.get('model')].browse(values.get('res_id')). name_get()
Read the requested field of the record, And it is a low level / RPC method. In Python code, use browse ().
Eg: self.env['mail.channel'].browse(self.group_groups.id).read()
Model.read_group(domain, fields, groupby, offset=0, limit=None, orderby=False, lazy=True)
Gets a list of records in the listview grouped by the specified group by field.
Eg: opportunity_data = self.env['crm.lead'].with_context(active_test = False).read_group(
    domain = [('partner_id', 'in', all_partners.ids)],
    fields = ['partner_id'], groupby = ['partner_id'])
3. Fields/Views
Model.fields_get([fields][, attributes])
Returns the definition of each field.
The return value is the dictionary  (indexed by field name). Contains the _inherits'd fields. Strings, help, and selections (if any) attributes are converted.
Eg: self.env['product.template'].fields_get()
Model.fields_view_get([view_id | view_type='form'])
Gets the detailed configuration of the requested view, including fields, models, and view architecture.
Eg: view = self.env.ref('hr.res_users_view_form_profile')
view_infos = james.fields_view_get(view_id=view.id)
4. Unlink
Delete the records in the current record set.
Eg: self.env['mail.channel'].browse(self.group_private.id).unlink()
The present () method returns a subset of the records that exist within itself. Returns a recordset containing only the records that exist in the database. It can also be used to check if a particular record still exists.
Eg: lead = self.env[active_model].browse(self.env.context.get('active_id')).exists()
The ensure_one() method checks if the record set contains only one record. If not, throw an exception.
Eg: self.ensure_one()
5. Filter
Which returns the record which satisfies func.
Eg: new_partner = message.partner_ids.filtered(lambda partner: partner.email == self.email_from)
6. Map
Apply func to all of its own records and return the result as a list or record (if func returns a record). In the latter case, the order of the returned records is arbitrary.
Eg: pickings = self.mapped('picking_ids')
7. Sort
Model.sorted(key=None, reverse=False)
Returns a self-ordered Recordset with the key.
Eg: tax_cash_basis_moves = res['tax_cash_basis_moves'].sorted(lambda move: move.tax_cash_basis_origin_move_id.id)
With ORM, you don't have to worry about creating databases or mapping fields between front-end databases. The table and its fields are automatically created based on the class you created in Odoo. You may also need to perform some calculations and perform some operations before adding them to the database. This can be overridden via the Odoo Orm method to perform such operations.

If you need any assistance in odoo, we are online, please chat with us.


Leave a comment




Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635



Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.



Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message