Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Correct way to create a domain with in condition and multiple & (AND) conditions?

on 5/11/16, 5:27 AM 468 views

Hi guys,

I've made some custom code that needs to filter out particular purchase invoices.
The domain has to look at multiple conditions:

  • The invoice has to be a purchase invoice, not a sales invoice (controlled by in_invoice or out_invoice on the type field from the model)

  • The state has to be either open or paid, not concept.

  • The create_date has to be bigger, or equal, than the start_date, which is a custom field I made.

  • The create_date has to be smaller, or equal, than the end_date, which is a custom field I made.

When I write a domain for getting only the purchase invoices that are only in the state open or paid it'll work and it looks like this:

invoices = self.env['account.invoice'].search((['&',('type', 'in', ['in_invoice', 'in_refund']),('state', 'in', ['open', 'paid'])]))

When I write a domain to only get purchase invoices that are created after start_date and that are created before the end_date it will work fine too:

invoices = invoices.search(['&',('create_date', '>=', self.start_date), ('create_date', '<=', self.end_date)])

However, when I now want to combine those two in one statement I get incorrect results. My domain with the combination of both domains merged:

invoices = self.env['account.invoice'].search(['&', '&', ('type', 'in', ['in_invoice', 'in_refund']),('state', 'in', ['open', 'paid']), ('create_date', '>=', self.start_date), ('create_date', '<=', self.end_date)])

However when I do this the domain does not seem to look at the start_date and end_date anymore.
So, what is wrong with this domain? I thought that the polish annotation read from left to right, meaning that my first '&' tells Odoo to both comply with the invoice state AND the dates, but this does not seem to be the case?
Could anybody explain me why this is the case and what I did wrong in this domain?


Juan Vicente Pascual
On 5/11/16, 6:56 AM

try this,

invoices = self.env['account.invoice'].search([('type', 'in', ['in_invoice', 'in_refund']),

                                                                           ('state', 'in', ['open', 'paid']),

                                                                           ('create_date', '>=', self.start_date),

                                                                           ('create_date', '<=', self.end_date)])

This works perfectly, thanks Juan! I actually didn't knew I didn't even need the '&' operators.

on 5/11/16, 9:49 AM
Fekete Mihai
On 5/11/16, 9:49 AM

Hi Yenthe, by default Odoo uses AND operator, so if you are not using OR operator, you should not add the operators in front, but if are doing, first operator is for last 2 conditions and so on, here you have 2 operators and 4 conditions...at 4 conditions you should have 3 operators...

I've converted your comment to an answer since the reason why is also very important. I didn't even know by default '&' was enforced for multiple domains like this. +1

on 5/11/16, 9:50 AM

Yenthe, here is an old topiuc describing more the search operators, please see https://www.odoo.com/forum/help-1/question/full-list-of-search-operators-46716

Fekete Mihai
on 5/11/16, 11:09 PM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 5/11/16, 5:27 AM
Seen: 468 times
Last updated: 5/11/16, 9:49 AM