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

Bidirectional many2many relation

By
Celia
on 5/25/16, 7:04 AM 290 views

This is my code: 

class tickets(osv.Model):    
    _name = 'tickets'   
    _rec_name = 'code'    
    _columns = {        
        'code': fields.char(string='Reference'),
        'title': fields.char(string='Title', required=True),
        'description': fields.text(string='Description', required=True),
        'status': fields.selection(
            [('draft', 'New'),
            ('open', 'In Progress'),
            ('pending', 'Pending'),
            ('validate', 'To Validate'),
            ('done', 'Closed'),
            ('cancel', 'Cancelled')], string='Status', required=True),
        'related_ticket_ids': fields.many2many('tickets', 'related_tickets_rel', 'ticket_id1', 'ticket_id2', string='Related tickets'),
    }

Imagine that we have 3 tickets created: ticket1, ticket2 and ticket3. 
What I want is when I add in ticket1 (in the 'related_ticket_ids' field) the ticket2, in the ticket2's 'related_ticket_ids' field appears ticket1. 


0

Ahmed M.Elmubarak

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


Ahmed M.Elmubarak
On 5/25/16, 7:55 AM

Hello,

Try to add new fields that represent the bidirectional fields as:

'bi_related_ticket_ids': fields.many2many('tickets', 'related_tickets_rel', 'ticket_id2', 'ticket_id1', string='Bidirectional Related Tickets')


Table name is same as your original table name: related_tickets_rel

and columns names is inverse ticket_id2 then ticket_id1

then this new field will be filled automatically

EDIT

if you don't want to add new filed, then I think you need to override the create & write methods as well


Hope this will be helpful 



Thanks Ahmed. I will override the create and write methods

Celia
on 5/25/16, 8:37 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: 5/25/16, 7:04 AM
Seen: 290 times
Last updated: 5/25/16, 7:55 AM