How to Use Create Method in Odoo

As we know that, Odoo is an open source ERP software where you can develop you erp system as per your business needs by developer. For that, developer needs to create records for further working. That's why we will discuss today, how to use create orm method in Odoo 16. Create method in Odoo, the create method is used to create new records in a model by using python programming language. 

How to Use Create Method in Odoo

How to Use Create Method in Odoo

Here's a basic example of how you can use the create method in Odoo:

Syntax:

This is a basic syntax to use create method in Odoo 16 by using python programming language.

   new_record = self.env['your.model'].create({
       'field1': value1,
       'field2': value2,
       # Add more fields as needed
   })

Example:

Let's say you have a model named library.book with fields name and author_id. In this example, the name field is char type while author_id is a field of type many2one referencing the res.partner model.
Note: The res.partner model is a base model, which is already available for customer and vendors by Odoo ERP.

In Python:

   new_book = self.env['library.book'].create({
       'name': 'Introduction to Odoo Development',
       'author_id': author_id,  # Assuming `author_id` is the ID of a record in `res.partner`
   })

You can create multiple records by passing a list of dictionaries to the create method in Odoo. If you have those fields who have required attribute which means you can not create record without required attribute fields, then you must have to add them first. 
You can also pass a context dictionary to the create method to influence the behavior of creating records, such as setting default values or triggering certain actions.
Previous Post Next Post