ERP Python and domain filter
ORM methods in Odoo
create()
The create() method is used to create a new record in the database. It takes a dictionary of field values as an argument and returns a new record object.
Example 1:
Suppose we have a model called Product with fields such as name, description, price, and category. To create a new product with the name "Red Apples", a description of "Fresh and Juicy", a price of 2.99, and a category of "Fruit", we would use the following code:
pythonnew_product = env['product'].create({ 'name': 'Red Apples', 'description': 'Fresh and Juicy', 'price': 2.99, 'category': 'Fruit', })
Example 2:
Suppose we have a model called Sale with fields such as customer, product, quantity, and total. To create a new sale for 3 units of Red Apples to customer John Doe, we would use the following code:
pythonproduct_id = self.env['product'].search([('name', '=', 'Red Apples')]).id customer_id = self.env['res.partner'].search([('name', '=', 'John Doe')]).id new_sale = self.env['sale'].create({ 'customer': customer_id, 'product': product_id, 'quantity': 3, 'total': 8.97, # assuming 2.99 is the price of one unit })
browse()
The browse() method is used to retrieve a record from the database based on its ID. It takes a single argument, the record ID, and returns a record object.
Example:
Suppose we have the ID of a product, and we want to retrieve its name and price. We would use the following code:
pythonproduct_id = 1 # assuming 1 is the ID of the product we want to retrieve product = self.env['product'].browse(product_id) product_name = product.name product_price = product.price
search()
The search() method is used to search for records in the database that match certain criteria. It takes a domain expression as an argument and returns a list of record IDs that match the criteria.
Example 1:
Suppose we want to retrieve all products with a price less than or equal to 5.99. We would use the following code:
pythonproduct_ids = self.env['product'].search([('price', '<=', 5.99)])
Example 2:
Suppose we want to retrieve all sales made by a customer named John Doe. We would use the following code:
pythoncustomer_id = self.env['res.partner'].search([('name', '=', 'John Doe')]).id sale_ids = self.env['sale'].search([('customer', '=', customer_id)])
read()
The read() method is used to retrieve field values for a record or a list of records. It takes a list of record IDs as an argument and returns a list of dictionaries containing the field values.
Example:
Suppose we have the IDs of several sales, and we want to retrieve the product name, quantity, and total for each sale. We would use the following code:
pythonsale_ids = [1, 2, 3] # assuming 1, 2, and 3 are the IDs of the sales we want to
write()
The write() method is used to update the field values of a record. It takes a dictionary of field values as an argument and updates the record accordingly.
Example usage:
pythonself.env['model.name'].browse(record_id).write({ 'field_name_1': new_value_1, 'field_name_2': new_value_2, })
---------------------------
Some Code Examples :
# Example 1 of create() new_product = self.env['product'].create({ 'name': 'Red Apples', 'description': 'Fresh and Juicy', 'price': 2.99, 'category': 'Fruit', }) print(new_product) # Output: # product(4,) # Example 2 of create() product_id = self.env['product'].search([('name', '=', 'Red Apples')]).id customer_id = self.env['res.partner'].search([('name', '=', 'John Doe')]).id new_sale = self.env['sale'].create({ 'customer': customer_id, 'product': product_id, 'quantity': 3, 'total': 8.97, # assuming 2.99 is the price of one unit }) print(new_sale) # Output: # sale(7,) # Example of browse() product_id = 1 # assuming 1 is the ID of the product we want to retrieve product = self.env['product'].browse(product_id) product_name = product.name product_price = product.price print(product_name, product_price) # Output: # Red Apples 2.99 # Example 1 of search() product_ids = self.env['product'].search([('price', '<=', 5.99)]) for product in product_ids: print(product.name, product.price) # Output: # Red Apples 2.99 # Bananas 0.99 # Example 2 of search() customer_id = self.env['res.partner'].search([('name', '=', 'John Doe')]).id sale_ids = self.env['sale'].search([('customer', '=', customer_id)]) for sale in sale_ids: print(sale.product.name, sale.quantity, sale.total) # Output: # Red Apples 3 8.97 # Example of read() sale_ids = [1, 2, 3] # assuming 1, 2, and 3 are the IDs of the sales we want to retrieve sales = self.env['sale'].browse(sale_ids) for sale in sales: print(sale.product.name, sale.quantity, sale.total) # Output: # Red Apples 2 5.98 # Bananas 5 4.95 # Red Apples 1 2.99
Introduction to Odoo Domain Filters
Odoo uses domain filters to specify search criteria for records in the database. Domain filters are a list of tuples, where each tuple specifies a search criterion. The domain filter is passed as an argument to the search() method of an Odoo model.
The general syntax of a domain filter is as follows:
arduino[(field_name, operator, value)]
where field_name is the name of the field to search, operator is the comparison operator, and value is the value to search for.
Here's an example of a domain filter that searches for all products with a price less than or equal to 5.99:
css[('price', '<=', 5.99)]
In this example, 'price' is the field name, '<=' is the operator, and 5.99 is the value.
Now let's look at how domain filters work with each data type.
Domain Filters with Integer Fields
For integer fields, the following comparison operators are available:
'=': equal to
'>=': greater than or equal to
'<=': less than or equal to
'>': greater than
'<': less than
'<>' or '!=': not equal to
Here's an example of a domain filter that searches for all products with a quantity greater than or equal to 10:
css[('quantity', '>=', 10)]
Domain Filters with Float Fields
For float fields, the same comparison operators are available as for integer fields.
Here's an example of a domain filter that searches for all products with a price less than or equal to 10.0:
css[('price', '<=', 10.0)]
Domain Filters with Char and Text Fields
For char and text fields, the following comparison operators are available:
'=': equal to
'=ilike': case-insensitive equal to (similar to SQL's ILIKE)
'!=ilike': case-insensitive not equal to
'like': case-sensitive like (similar to SQL's LIKE)
'not like': case-sensitive not like
'ilike': case-insensitive like
'not ilike': case-insensitive not like
Note that the ilike operator uses the % wildcard character, so you can search for substrings. For example, to search for all products whose name contains the word "apple", you could use the following domain filter:
css[('name', 'ilike', '%apple%')]
Domain Filters with Date and Datetime Fields
For date and datetime fields, the following comparison operators are available:
'=': equal to
'>=': greater than or equal to
'<=': less than or equal to
'>': greater than
'<': less than
'<>' or '!=': not equal to
Here's an example of a domain filter that searches for all sales made in January 2022:
css[('date', '>=', '2022-01-01'), ('date', '<=', '2022-01-31')]
Note that dates are specified as strings in the format 'YYYY-MM-DD'.
Domain Filters with Boolean Fields
For boolean fields, the following comparison operators are available:
'=': equal to
'!=': not equal to
Here's an example of a domain filter that searches for all active customers:
css[('active', '=', True)]
This will return all records where the active field is set to True.
You can also use the '!=' operator to find all inactive customers:
css[('active', '!=', True)]
This will return all records where the active field is not set to True.
Combining Domain Filters
You can combine multiple domain filters using the & (AND) and | (OR) operators. Here's an example of a domain filter that searches for all products with a quantity greater than or equal to 10 and a price less than or equal to 10.0:
css[('quantity', '>=', 10), ('price', '<=', 10.0)]
This will return all records that satisfy both conditions.
If you want to search for records that satisfy either condition, you can use the | operator:
css[('quantity', '>=', 10) | ('price', '<=', 10.0)]
This will return all records that satisfy either condition.
Potential Issues and How to Resolve Them
One potential issue when using domain filters is that you might get unexpected results if you use the wrong operator or value. For example, if you use the '=' operator with a float field, you might not get any results because of rounding errors.
Another potential issue is that domain filters can be slow if you have a large number of records in the database. To improve performance, you can use indexed fields or limit the number of records returned using the limit argument of the search() method.
Potential issues and how to resolve them
Record does not exist
If you try to perform an operation on a record that does not exist in the database, you will get a "Record does not exist" error. To resolve this issue, you can use the create() method to create a new record with the specified field values.
Domain expression is invalid
If your domain expression is invalid, you will get a "Invalid domain expression" error. To resolve this issue, you should check your domain expression and make sure that it is correctly formatted.
Record is already deleted
If you try to perform an operation on a record that has already been deleted, you will get a "Record does not exist" error. To resolve this issue, you should check whether the record still exists in the database before performing the operation.
Record is locked
If you try to perform an operation on a record that is currently locked by another user, you will get a "Record is locked" error. To resolve this issue, you should wait until the record is unlocked before trying again.
Consider telling a great story that provides personality. Writing a story with personality for potential clients will assists with making a relationship connection. This shows up in small quirks like word choices or phrases. Write from your point of view, not from someone else's experience.
Great stories are for everyone even when only written for just one person. If you try to write with a wide general audience in mind, your story will ring false and be bland. No one will be interested. Write for one person. If it’s genuine for the one, it’s genuine for the rest.
Rating
0
0
There are no comments for now.
Join this Course
to be the first to leave a comment.