Last active
June 23, 2023 14:08
-
-
Save altela/ee192adae4d7ff4039fb5d0ba0e341c0 to your computer and use it in GitHub Desktop.
Odoo ORM .search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# .search usability is to find object inside a model | |
# this block below means "search product_id with value 5 inside sale_order table" | |
# in postgresql query this ORM block equal to "SELECT * FROM sale_order_lines WHERE product_id = 5" | |
# then show all result using FOR iteration, preventing singleton error | |
for result in self.env['sale.order.lines'].search([('product_id', '=', 5)]): | |
product_id = int(result.product_id) # Get result.product_id and assign in to product_id inside current class | |
price_unit = int(result.price_unit) # Get result.price_unit and assign in to price_unit inside current class | |
print(product_id) | |
print(price_unit) | |
# another example is that we can also assign self.env into an object (variable) | |
# before assigning with kind of style, make sure that the object you wish to pull is only has single record | |
# or else "expected singleton error" will occur | |
result = self.env['sale.order'].search([('partner_id', '=', 1)]) | |
print(result.partner_id) | |
# to find record with several criteria, you can add domains to narrow the fetch result | |
# domain filter is unlimited | |
# this will search all record inside sale_order which has partner_id equals to 1 AND warehouse id equals to 1 | |
# can also be applied using FOR iteration | |
result = self.env['sale.order'].search([('partner_id', '=', 1), ('warehouse_id', '=', 1)]) | |
print(result) | |
# other ways can also be applied using this commands | |
# this will search qty_invoiced that has value of 5 inside the sale_order_lines which has id equals to 1 | |
# and then it will ONLY return qty_delivered that only show 3 records (since we set limit=3) | |
for qty_delivered in self.env['sale.order.lines'].search([('id', '=', '1'), ('qty_invoiced', '=', '5')], limit=3).qty_delivered: | |
print(qty_delivered) | |
# To search and return the certain field, it can be done by adding .mapped('field_name') | |
example = self.env['product.template'].search([('name', 'ilike', 'broom')]).mapped('name') | |
# This is the example of doing ORM search from QWEB | |
<t t-set="invoice_awal" t-value="docs[0]"/> | |
<b>No. Tagihan:</b> | |
<p class="m-0"> | |
<t t-if="invoice_awal"> | |
<t t-foreach="request.env['res.partner'].search([('name', 'like', invoice_awal.partner_id.name)])" t-as="obj"> | |
<t t-set="new_sequence" t-value="obj.dt_sequence + 1"/> | |
<t t-set="update_vals" t-value="{'dt_sequence': new_sequence}"/> | |
<t t-set="result" t-value="obj.write(update_vals)"/> | |
</t> | |
# This also an example to search the current Invoice number to header | |
<t t-foreach="request.env['account.move'].search([('id', '=', o.id)])" t-as="obj"> | |
<t t-esc="obj.name"/> | |
<t t-esc="obj.invoice_origin"/> | |
</t> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment