Globalteckz

Common Odoo ORM Methods

  • Home
  • Common Odoo ORM Methods
odoo orm methods

Common Odoo ORM Methods

Understanding Common Odoo ORM methods but before that first let’s understand  What is ORM –  Object-Relational Mapping also called as O/RM or O/R mapping tool. Its a programming technique used to convert data between incompatible type system by using Oriented Programming languages. With the same one can create in effect, a virtual object database that can be used from within the programming language.

Today there are free as well as enterprise packages available for performing ORM and some developers use their own ORM tools by opting to construct them.

ORM Methods in Odoo –

Odoo modeling is based on “objects” but its data is stored in a classical relational database named Postgresql. It is use to fill the gap between Open-objects and SQL tables. Python is the programming language giving the behavior and data description of Open-objects (This is not stored in the database). “ORM” is a python class ancestor of all Open-objects. An Open-object is modeling by a static.

Common Odoo ORM Methods:

  1. search()

Takes a search domain, returns a recordset of matching records. Can return a subset of matching records (offset and limit parameters) and be ordered (order parameter):

self.search([(‘is_company’, ‘=’, True)], limit=1)

Return : res.partner(7, 18, 12, 14, 17, 19, 8, 31, 26, 16, 13, 20, 30, 22, 29, 15, 23, 28, 74)

  1. search_count()

To just check if any record matches a domain, or count the number of records which do, use

Return : Count of record which match domain

  1. create()

            Takes a number of field values, and returns a recordset containing the record created

self.create({‘name’: “New Name”})

Return : res.partner(78)

  1. write()

Takes a number of field values, writes them to all the records in its recordset. Does not return anything.

self.write({‘name’: “Newer Name”})

  1. browse()

Takes a database id or a list of ids and returns a recordset, useful when record ids are obtained from outside Odoo (e.g. round-trip through external system)

self.browse([7, 18, 12])

Return : res.partner(7, 18, 12)

  1. exists()

Returns a new recordset containing only the records which exist in the database. Can be used to check whether a record (e.g. obtained externally) still exists.

            if not record.exists():

                        raise Exception(“The record has been deleted”)

  1. ref()

                        Environment method returning the record matching a provided external ID.

                        env.ref(‘base.group_public’)

                        Return : res.groups(2)

  1. ensure_one()

checks that the recordset is a singleton (only contains a single record), raises an error otherwise:

records.ensure_one()

# is equivalent to but clearer than:

assert len(records) == 1, “Expected singleton”

Benefits & Advantages of ORM Tools

1. ORM tools facilitates implementing the Domain Model pattern .

This one reason supercedes all others, using this pattern means that your model entities based on real business concepts rather than based on your database structure. ORM provide this functionality through mapping between the logical business model and also the physical storage model.

2. Reduction in Code

ORM tools provide a host of services thereby allowing developers to focus on the business logic of the application rather than repetitive CRUD (Create Read Update Delete) logic.

3. Changes to the object model are made in one place.

Once you update your object definitions, the ORM will automatically use the updated structure for retrievals and updates. There are no SQL Update, Delete and Insert statements strewn throughout different layers of the application that need modification.

4. Rich query capability.

ORM tools provide an object oriented query language. This allows application developers to focus on the object model and not to have to be concerned with the database structure or SQL semantics. The ORM tool itself will translate the query language into the appropriate syntax for the database.

5. Navigation.

You can navigate object relationships transparently. Related objects are automatically loaded as needed. For example if you load a PO and you want to access it’s Customer, you can simply access PO.Customer and the ORM will take care of loading the data for you without any effort on your part.

6. Data loads

Data Loads are completely configurable allowing you to load the data appropriate for each scenario. For example in one scenario you might want to load a list of POs without any of it’s child / related objects, while in other scenarios you can specify to load a PO, with all it’s child Line Items, etc.

7. Concurrency support.

Support for multiple users updating the same data simultaneously.

8. Cache management.

Entities are cached in memory thereby reducing load on the database.

9. Transaction management and Isolation

All object changes occur scoped to a transaction. The entire transaction can either be committed or rolled back. Multiple transactions can be active in memory in the same time, and each transactions changes are isolated form on another.

10. Key Management.

Identifiers and surrogate keys are automatically populated and managed.

ORM frameworks provide a way to interact with a relational database using objects in an object-oriented programming language. It allows developers to work with objects in their code and then automatically translates the data between the objects and the database tables. This mapping is typically defined using metadata or configuration, and it helps abstract away the complexities of SQL queries and database schema details.

Key concepts in ORM include:

  1. Entity: An entity represents a table in the database. In OOP, an entity is often mapped to a class.
  2. Attributes/Properties: These are the fields or columns in a table, which are represented as properties of an object in OOP.
  3. Relationships: ORM allows you to define relationships between entities, such as one-to-one, one-to-many, or many-to-many relationships.
  4. Mapping: The process of defining how the objects in the code correspond to the tables and columns in the database.

Popular ORM frameworks for various programming languages include:

  • Hibernate: for Java
  • Entity Framework: for .NET
  • Django ORM: for Python
  • Active Record: used in Ruby on Rails
  • Spring Data JPA: for Java with the Spring framework

Conclusion:

With ORM we don’t need to take care about database creation and mapping of the fields between Frontend database. It auto create table and its fields based on classes which we are creating in Odoo. Further, Some times we need to do some calculations and perform some operation before it will add to database which we can overwrite via odoo orm method and manage such operations.

Our Articles related to Odoo 

Leave Comment

×