Skip to Content
Menu
This question has been flagged
3 Replies
3145 Views

I have a new model:

# -*- coding: utf-8 -*-
from openerp.osv import osv, fields
class test_branch(osv.osv):
 _name = "test.branch"
 _columns = { 'id': fields.char(string='ID'), 'name': fields.char(string='Name') }


Now when I make on another model an many2many relation to this:


class test_branch_rel(osv.osv):

_name = 'test.branch.rel'

_columns = { 'branch_left_id': fields.many2one('test.branch'),

'branch_right_id': fields.many2one('test.branch', 'Relationed Branch'), }


'test_branch': fields.many2many('test.branch', 'test_branch_rel', 'branch_left_id', 'branch_right_id'),
'test_zielbranch': fields.many2many('test.branch', 'test_branch_rel', 'branch_right_id', 'branch_left_id')


I can't save that many2many field:

I get this error:

integrity errors

[a reference object: test.branch - test.branch]


Avatar
Discard
Best Answer

Hi,

the correct syntax is
'test_branch': fields.many2many('test.branch', 'test_branch_rel', 'branch_left_id', 'branch_right_id'),


but you didnt defined 'branch_left_id'. you have to make these also as a many2one field.


Avatar
Discard
Author

where I need to define branch_left_id ?

In this model _name = 'test.branch.rel'

Best Answer

Hi,

The syntax for Many2Many fields is :-

    fields.Many2many(comodel_name=None, relation=None, column1=None, column2=None, string=None)

Here comodel_name is the name of target model,

and relation is name of table that store relation in database. ie it will create a table in database with the given name, which store the relation,

column1 and column2  are the name of colums of relation-table in database.


Hope this helps.


Avatar
Discard
Related Posts Replies Views Activity
0
May 16
4806
1
Mar 15
4713
0
Dec 22
1520
4
Jun 22
18642
2
Sep 17
8565