10

Can someone explain to me something about related fields. For example -

  • How it was used
  • How it can be helped
  • For which kind of scenario I should use fields.related

If anybody can provide a small example for real use of fields.related I would appreciate it.

Reg
  • 10,717
  • 6
  • 37
  • 54
necromancer
  • 6,477
  • 5
  • 18
  • 13

4 Answers4

9

It lets you pull a field from a related table. You can find more details in the developer book, and one example to look at is the order_partner_id field of the sale_order_line class. In version 5.14, that's at line 806 of addons/sale/sale.py.

I often find that I want to display a field in a list, but it's on a parent record instead of the actual table that I'm listing.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286
  • I was thinking the same, I've tried the example from developer book too, its great, in prior version I use to achieve this by function field - great to have related fields now - it works upto n level ! bingo !!! - Thanks – necromancer Oct 09 '10 at 04:24
8

When using a related field you have to first select which field to be related. For example I'm creating a new module for adding student details. Here the student is actually the partner. So _rec_name='partner_id' is taken.In res.partner you may have seen the ref field. The value in the ref field is taken as the internal_number for the student module.

So what we do here is

class student(osv.osv):
    _name='student'
    _rec_name='partner_id'
    _columns ={
           'partner_id':fields.many2one('res.partner','Name'),
           'internal_number':fields.related(
                   'partner_id',
                   'ref',
                   type='char',
                   size=16,
                   string='Internal Number',
                   ),
           }

If the field we want to show as related field is a selection field, then you have to provide type='selection', and selection=[(case1,case1),(case2,case2),...], a list of tuples. If it is a many2one field, then type='many2one' and relation='model_name'.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
OmaL
  • 5,037
  • 3
  • 31
  • 48
  • 'country_id':fields.related('partner_id', 'country_id', type='many2one',relation='res.country', string='Country'), – OmaL Feb 16 '15 at 12:49
1

related fields leads the control to another table the parent table wil have a onetomany relation with the child table and the child table have a manytoone relation towards the parent table. eg: the account.invoice to account.invoice.line with the following field

'invoice_line': fields.one2many('account.invoice.line', 'invoice_id', 'Invoice Lines', readonly=True, states={'draft':[('readonly',False)]}),

and account.invoice.line relates to account.invoice with the following code in reverse.

'invoice_id': fields.many2one('account.invoice', 'Invoice Reference', ondelete='cascade', select=True),

Pravitha V
  • 3,308
  • 4
  • 33
  • 51
1

You can find an example in OpenERP developer documentation, in database normalization it's called Transitive dependency.

Don Kirkby
  • 53,582
  • 27
  • 205
  • 286