Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
4 Risposte
12203 Visualizzazioni

Please help me to understand the basic concepts of releated field in openerp 7.0 with simple examples

Avatar
Abbandona
Risposta migliore

A related is simply a function field capable to retrieve by itself values from relations models, typically from many2one relation models, for this you need to specify the path to the field in the target model that you want to access using the related field. This path is in the form of a sequence of arguments for the constructor of field at the begining, the rest of the arguments that not are part of the path need to be named arguments. As every function field the related need to specify it's type and in the case when the target type is a relation one like many2one, one2many or many2many, you need to specify of the relation named value with the target model of the relation. Take this examples, we will create related fields to access fields through many2one relations of the models:

class res_example1(osv.osv):
_name = 'res.example1'
_columns = {
'name': fields.char('Name', size=64),
'code': fields.char('Code', size=64),
'value': fields.float('Value'),
}
res_example1()

class res_example2(osv.osv): 
_name = 'res.example2'
_columns = {
'name': fields.char('Name', size=64),
'example_id': fields.many2one('res.example1', string='Example 1'),
'code_example': fields.related('example_id', 'code', type='char', string='Code'),
}
res_example2()

class res_example3(osv.osv): 
_name = 'res.example3'
_columns = {
'name': fields.char('Name', size=64),
'example_id': fields.many2one('res.example2', string='Example 2'),
'value_example': fields.related('example_id', 'example_id', 'value', type='float', string='Value'),
'ref_example': fields.related('example_id', 'example_id', type='many2one', relation='res.example1', string='Example 1'),
}
res_example3()

In res.example2 model the related access to the code field of the res.example1 through it's field example_id, using this path ('example_id', 'code',...) as first parameters in the field definitions. In res.example3 model we have 2 related fields, the value_example is for access the field value in res.example1, note that res.example3 has no direct relation with res.example1, thas why the arguments for path is ('example_id', 'example_id', 'value',...). Using the relation with res.example2 and res.example2 relation with res.example3 is how res.example3 related field value_example have access to the field value of res.example1. The second related in res.example3 is for show how a related to a relation target type can be done, in this case we will map the access to the field example_id in res.example2 so we need to specify the type many2one and the target relation using the relation argument.

Hope this helps

Avatar
Abbandona

does it the same in V8 ?

yes, you could do it using the old api or v8 api like:

nickname = fields.Char(related='user_id.partner_id.name')
Risposta migliore

Axel Mendoza's answer says it all, as an extra a simplified description would be:
* A related field is a link to another field stored in another model, as axel said, usually in a m2o relation field

Avatar
Abbandona
Post correlati Risposte Visualizzazioni Attività
2
mag 22
33796
0
mar 19
3944
0
gen 19
4988
0
gen 18
3866
0
set 17
4279