简体   繁体   中英

Get values from a parent field - Odoo10

I have 2 fields F1 and F2 in a form. Field F1 is a popup. On popping up it has 2 more fields F3 and F4. I need to access value stored in F3 and store it in F2.

Attached image might help to understand the workflow. Click to view this image

Here are some more details to my question

Here is what I tried.

class JobJob(models.Model):
    _name='job.job'
    address = field.Char('Address')


Class Dailylog(models.Model):
    job_id = fields.Many2one('job.job', 'Job')
    def check(self):
        address = fileds.Char(related='job_id.address')

If I got it right your question, you want to use relation field. In my code, you use your relation with the MyModel to get f3 and f4 , so you don't have to store it. (Of course, you can, if you take store=True in the relation filed definition, when you don't store it, you can't search in that fields.)

class MyModel(models.Model):
    _name = 'my.model'

    f3 = fields.Char(string='F3')
    f4 = fields.Char(string='F4')

class MyOtherModel(models.Model):
    _name = 'my.other.model'

    f1 = fields.Many2one('my.model', 'F1')
    f2 = fields.Char('F2')
    f3 = fields.Char(related='f1.f3')
    f4 = fields.Char(related='f1.f4')

Now you have both field ( f3 and f4 ), so easy to show them on your form.

EDIT ( Question changed )

TL;DR : You can't define field in a method.

I know this is just a logical field, but your code is a generic change on relation model. If you could do that, that means you can change database schema on the fly. So sometimes one times address field exits, sometime not...but when it it isn't exist, it will wipe earlier loaded data in to this field. This may a huge inconsistency risk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM