Odoo Help

Welcome!

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.

0

How can I filter for empty / non-empty one-to-many relations?

By
Peter Nietz
on 4/1/15, 4:22 AM 978 views

I have two models that have a many-to-one relationship:

class model_a(models.Model):
...
model_b_id = fields.Many2one('Some name', comodel_name='model_b')
...
class model_b(models.Model):
...
model_a_ids = fields.One2many('Some name', comodel_name='model_a', inverse_name='model_b_id', readonly=True)
...

Now my goal is to have two filters, one showing only those entities of model B that have at least one entity of model A assigned (model_a_ids is not empty), and one showing only those entities of model B that have no entity of model A assigned (model_a_ids is empty).

I managed to acheive the first goal with the following filter:

<filter string="Assigned" domain="[('model_a_ids', '!=', [])]"/> 

But my attempts to create the opposite filter failed. I tried the following:

<filter string="Unassigned 1" domain="[('model_a_ids', '=', [])]"/>
<filter string="Unassigned 2" domain="['!', ('model_a_ids', '!=', [])]"/>

Both filters do not work, with both filters the result is empty (although there are entities fulfilling this criterion). And there is no error message in the log either.

How can I achieve my goal?



1

Ahmed M.Elmubarak

--Ahmed M.Elmubarak--
2988
| 5 3 5
Sudan
--Ahmed M.Elmubarak--


Ahmed M.Elmubarak
On 4/1/15, 8:56 AM

Hi,

Try

<filter string="Unassigned 1" domain="[('model_a_ids', '=', False)]"/>

It will work

Regards....

Wow, great, that works like a charm! Many thanks!

Peter Nietz
on 4/1/15, 10:02 AM

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)

Stats

Asked: 4/1/15, 4:22 AM
Seen: 978 times
Last updated: 4/1/15, 10:01 AM