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 to create a many2one based on a many2many relation?

By
Henrique de Paula Lopes
on 11/3/16, 1:26 PM 195 views

Hey there!

Let's suppose that I'm working on a system that allows the user to create a product and assign different tags for that product (as a many2many relationship - a lot of tags already exist and the user can assign as many as he wants to a specific product). I want, however, to also create a field that will make one of these assigned tags "special". I want the user to be able to select one tag among all the tags he have previously assigned to this specific product. How can I do that? so far, I've tried to create a many2one field with a "compute" function, returning all tags assigned to the product as a list of (id, tag_name) pairs, but it didn't work. 


Anyone know how to do it? Thanks in advance.

4
Ameera PP
On 11/4/16, 2:22 AM

Hello Henrique,

Try this:

create fields in python file :

'contact_person_many': fields.many2many('dispatch.contact.person', string='Contact Persons', required=True),        'contact_person_one': fields.many2one('dispatch.contact.person', string='Contact Person'),


def onchange_contact_person_many(self, cr, uid, ids, contact_person_many, context=None):
        if contact_person_many:
            li_emp = []

            for i in contact_person_many[0][2]:
                li_emp.append(i)           

            emp = tuple(li_emp)     

           return {'domain': {'contact_person_one': [('id', 'in', emp)]}}

     In xml file:

<field name="contact_person_many" widget="many2many_tags" on_change="onchange_contact_person_many(contact_person_many)"/>                      

<field name="contact_person_one"/>

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: 11/3/16, 1:26 PM
Seen: 195 times
Last updated: 11/5/16, 1:16 AM