Best way to find relations


(Hendrik Brandes) #1

Hello,

we are often facing the situation to search for specific categories - Party or Product.
Actual we solve this often with a

for category in product.categories:
    if category == searched_category:
         ....

( replace the condition with a specific one )
Is it possible to make this easier by an Pyson domain condition like:

specific_products = Product.search([('categories','in',list_of_searched_categories)])

Thank you and best regards,
Hendrik


(Sergi Almacellas Abellana) #2

This expression is totally valid and the tryton ORM will generate the queries to filter the especific categories. Indeed this is recomended because filtering is faster on database than on python.

You can also use the domain to search on fields of target relations. Please have a look at the domain documentation


(Hendrik Brandes) #3

Thank you. Perhaps, I need more to work with the pyson expressions, they are not so easy to read for me…

I am not sure why the last domain clause does not worked for me.
What kind of elements needs to be in the list of searched categories: a) the IDs or b) the instances itself?


(Nicolas Évrard) #4

Beware: domains are not PYSON expressions.

Domains are roughly a way to specify the WHERE part of a SQL query.
PYSON expressions are a way to transmit Python expressions over the network (think of them as JSON expressions but for python). Of course you can use python expressions to specify some boolean value which is what is used in states (where you usually meet it).


(Sergi Almacellas Abellana) #5

What is not working? Do you have some crash or no results are returned? Are you sure there are products in this category? Take in account that accounting category is not included in categories

It should work with both objects, the instances and the ids