简体   繁体   English

从父字段获取值-Odoo10

[英]Get values from a parent field - Odoo10

I have 2 fields F1 and F2 in a form. 我有2个字段F1和F2的形式。 Field F1 is a popup. 字段F1是一个弹出窗口。 On popping up it has 2 more fields F3 and F4. 弹出时,它还有2个字段F3和F4。 I need to access value stored in F3 and store it in F2. 我需要访问存储在F3中的值并将其存储在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. 在我的代码中,您可以使用与MyModel的关系来获取f3f4 ,因此不必存储它们。 (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.) (当然,如果您在关系字段定义中采用store=True ,则可以不存储它而不能在该字段中进行搜索。)

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. 现在您同时拥有两个字段( f3f4 ),因此很容易在表单上显示它们。

EDIT ( Question changed ) 编辑问题已更改

TL;DR : You can't define field in a method. TL; DR :您无法在方法中定义字段。

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. 这可能会带来巨大的不一致风险。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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