This question has been flagged
2 Replies
16139 Views

I was looking at the example of the OpenERP tutorial:

'country_id': fields.related(
    'state_id',
    'country_id',
    type="many2one",
    relation="res.country",
    string="Country",
    store=False)

And I'm trying to replicate it with my own fields:

'meeting_id': fields.many2one('event.meeting', 'Meeting', ondelete='cascade'),

'event_id': fields.related(
    'meeting_id',
    'event_id',
    type='many2one',
    relation='event.meeting',
    string='Event'),

But I'm getting this error:

KeyError: 4

However, the event I want to get in that field has 4 as its ID, so, what is happening? (the rows in the table event_meeting have a column named event_id, which is a many2one field pointing to the id of the table event_event).

Avatar
Discard

meeting_id need to be another field in the same model as event_id is defined. event_id need to be a field in the model that is represented by meeting_id. 'event.meeting' need to be the model that event_id (the field in the meeting_id) represents.

Best Answer

Hi,

Just make your related fields as like below.

'meeting_id': fields.many2one('event.meeting', 'Meeting' ),

'event_id': fields.related('meeting_id', 'event_id', type='many2one', relation='event.event', string='Event', readonly=True, store=True),

If you want to make realted fields for M2O then you have to give the relation of the last field, here 'event_id' is the last field so, you have to give the relation is 'event.event'.

I hope it will resolve your issue.

Avatar
Discard
Author

That's right! I had to write relation='event.event' instead of relation='event.meeting'. Thank you!

Best Answer

Hi,

Here is a code for your question, try this example.

        'company_id': fields.many2one('res.company', 'Company', select=1),

        'company_currency': fields.related('company_id', 'currency_id', type='many2one', string='Currency', relation="res.currency"),

I hope this will help for you...

Avatar
Discard