What is the use of related fields in OpenERP?
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开发者_如何学C use fields.related
If anybody can provide a small example for real use of fields.related I would appreciate it.
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.
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'
.
You can find an example in OpenERP developer documentation, in database normalization it's called Transitive dependency.
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),
精彩评论