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.

1

How to check whether two many2many fields are exactly equal or not in odoo9?

By
Sebin Siby
on 8/3/16, 3:15 AM 316 views

Hi,


I have a many2many field "tag_ids" both in models "news.ads" and "blog.post".

I want to get all the records from "blog.post" whose "tag_ids" exactly matches the "tag_ids" in "news.ads".

I tried the following in my controller but it doesn't worked,

blog_obj = request.registry['blog.post']

p_id = blog_obj.search(cr, uid, ['&', ['id','=',post_id], ['website_published', '=', True]], context=context)

post = blog_obj.browse(cr, uid, p_id, context=context)


ad_obj = request.registry['news.ads']

banner_ads = ad_obj.search(cr, uid, [('state', '=', 'publish'), ('tag_ids', 'in', [post.tag_ids])], context=context)

How do I search such records in odoo9? Any workaround..!!

1

Pawan

--Pawan--
1267
| 4 3 5
Hyderabad, India
--Pawan--


Pawan
On 8/9/16, 7:43 AM

sebin,

what you can do is,

First get the records of both the object as:

news_ads_tags = self.env['news.ads'].browse(news_ads_id).tag_ids

blog_post_tags = self.env['blog.post'].browse(blog_post_id).tag_ids

now to get the ids which are in both of them:
res = [rec.id for rec in new_ads_tags if rec in blog_post_tags]

res will contains  all those tags which are in new_ads_tags and blog_post_tags both!!

Hope it helps!!



Hi Pawan,

Thanks for your answer. Actually I need to use it in a domain for a search operation. I updated my question above. How can I use it in the search operation.

Sebin Siby
on 8/10/16, 2:17 AM

Sebin,

first p_id is the list returned from search, so either you loop through it or for first element use p_id[0], then you can get the first records object form list using:

post = blog_obj.browse(cr, uid, p_id[0], context=context)

then in banner_ads you can try this:

banner_ads = ad_obj.search(cr, uid, [('state', '=', 'publish'), ('tag_ids', 'in', [id.id for id in post.tag_ids])],

Pawan
on 8/10/16, 7:45 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: 8/3/16, 3:15 AM
Seen: 316 times
Last updated: 9/6/16, 9:46 AM