WWW Cybrosys Com Blog Orm Methods in Odoo 15
WWW Cybrosys Com Blog Orm Methods in Odoo 15
AUG 16,2022
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.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Here are some common ORM methods for the Odoo:
1. create/update
Model.create(vals_list)
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'})
Model.default_get(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
Model.name_create(name)
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')
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Model.write(vals)
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
Model.browse([ids])
employee.employee_id = self.env['hr.employee'].browse(employee.id)
Eg:
self.env['crm.lead'].search([
('phone_mobile_search', 'like', '0499223311'))]
Model.search_count(args)
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', '=', comp
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
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
Model.read([fields])
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()
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
domain = [('partner_id', 'in', all_partners.ids)],
fields = ['partner_id'], groupby = ['partner_id'])
3. Fields/Views
Model.fields_get([fields][, attributes])
The return value is the dictionary (indexed by field name). Contains the _inherits'd
fields. Strings, help, and selections (if any) attributes are converted.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
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
Model.unlink()
Eg:
self.env['mail.channel'].browse(self.group_private.id).unlink()
Model.exists()
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
Model.ensure_one()
The ensure_one() method checks if the record set contains only one record. If not,
throw an exception.
Eg:
self.ensure_one()
5. Filter
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Model.filtered(func)
Eg:
new_partner = message.partner_ids.filtered(lambda partner: partner.email == se
6. Map
Model.mapped(func)
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)
Eg:
tax_cash_basis_moves = res['tax_cash_basis_moves'].sorted(lambda move: move.ta
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.
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
Share this article:
Related Blogs
0 Comments
Leave a comment
Name
Website
Comment
Post Comment
CALICUT LONDON
Cybrosys Technologies Pvt. Ltd. Cybrosys Limited
Neospace, Kinfra Techno Park Alpha House,
Kakkancherry, Calicut 100 Borough High Street, London,
Kerala, India - 673635 SE1 1LB, United Kingdom
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
KOCHI BANGALORE
Cybrosys Technologies Pvt. Ltd. Cybrosys Techno Solutions
1st Floor, Thapasya Building, The Estate, 8th Floor,
Infopark, Kakkanad, Dickenson Road,
Kochi, India - 682030. Bangalore, India - 560042
SEND US A MESSAGE
Full Name
Phone
Your Message
SEND
QUICK LINKS
Odoo
Odoo Apps
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com
pp
Odoo Partners
Contact us
Sitemap
SERVICES
Odoo Customization
Odoo Implementation
Odoo Integration
Odoo Support
Odoo Migration
Odoo Consultancy
Odoo Training
Odoo Licensing
REFERENCE
Odoo ERP
Odoo Software
Odoo vs SAP
Odoo vs Dynamics
Odoo vs Netsuite
Odoo vs Sage
STAY IN TOUCH
+91 8606827707
+91 8606827707
CONNECT SOCIALLY
Convert web pages and HTML files to PDF in your applications with the Pdfcrowd HTML to PDF API Printed with Pdfcrowd.com