Odoo:从many2one访问一列 [英] Odoo : Acces a column from many2one

查看:310
本文介绍了Odoo:从many2one访问一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的问题:

我在model1中:

class model1(osv.osv):
    _name = 'model1'
    _columns = {
        'name': fields.many2one('res.partner',
                                       u'Person',
                                       domain=[('my_boolean', '=', False)],
                                       required=True,
                                       select=True),
        'type' = fields.selection([('1', 'One'),
                                  ('2', 'Two'),
                                  u'Select',
                                  required=True),
     }

    @api.onchange('type')
    def onchange_type_model1(self)
        self.name.model3_id = 2

我想在此onchange中修改res.partner中model3_id的id的值:

And I want to modify in this onchange the value of the id of model3_id in res.partner:

-res.partner在此模块中:

-res.partner in this module:

class res_partner(osv.osv):
    _inherit = 'res.partner'
    _columns = {
        'model1_partner_ids': fields.one2many('model1',
                                                     'name',
                                                     u'asker from model1',
                                                     select=True),
    }

-res.partner在具有model3的模块中:

-res.partner in the module with model3:

class res_partner:
    _inherit = res.partner
    _columns = {
        'model3_id': fields.many2one('model3',
                                            u'Model3'
                                            track_visibility='onchange',
                                            select=True),
    }

model3在这里并不重要,我有10个id的model3对象,例如,当在model1中修改"type"时,我需要能够影响res.partner中的id"2"到"model3_id".

model3 is not important here, I have 10 id of model3 objects and I need to be able to affect, for example, the id " 2 " to 'model3_id' in res.partner when "type" is modified in model1.

我尝试了这个方法: odoo-通过执行来自many2one字段的值这个:

I tried this : odoo - get value from many2one field by doing this:

@api.onchange('type')
def onchange_type_demand(self, cr, uid, ids, context=None):
    for name in self.browse(cr, uid, ids, context=context):
        if name.model3_id:
            name.model3_id = 2

但是它给我一个错误,说record.env不存在.我试图找到此错误的答案,但没有一个与我的代码匹配.

But it give me an error saying that record.env doesn't exist. I tried to find answer to this error but none matched with my code.

我查看了数十个主题,一整天都在尝试修复它,但是无论我做什么,它都行不通. (我做的第一件事-出现在我写的第一堂课中-没有给我任何错误,但是数据库中没有任何改变.)

I looked to dozens of topics, tried all the morning to fix it, but whatever I do, it won't works. (The first thing I did -appear in first class I wrote- didn't gave me any error but changed nothing in the database.)

感谢您阅读和提供任何帮助.

Thanks for reading and for any help you could provide.

我认为这里唯一真正的问题是如何将ID作为many2one的值?"但是我不确定这是这里唯一的问题,所以我让所有文本提交.

I think the only real question here is "How to give an ID as a value for a many2one?" But I'm not sure that this is the only problem here so I let all the text.

推荐答案

我终于解决了我的问题:

I finally solved my problem :

创建onchange时,它暗示它只会更改javascript,如果您尝试修改的字段(由于其他字段上的onchange)不在您的树中,则不会进行更改.我想出了什么,在看phppgadmin时做了一些尝试:

When you create an onchange, it seams that it will only change the javascript, if the field you try to modify, thanks to an onchange on an other field, isn't in your tree, it won't be changed. What I figured out, doing some attempt while looking at phppgadmin:

  • 当您更改字段时,它会更新javascript,而数据库中不会执行任何操作.
  • 保存更改后,javascript会进行接缝以分析字段并使用这些字段更新数据库.

因此,当xml中没有某些内容时,就不能在其上使用onchange.如果您要更改其他模型或模块中的某些内容,这是一个很大的痛苦.但是更多的尝试是愚蠢的,这完全是有道理的:onchange的目的是在完成任何注册之前进行更新,唯一的兴趣是在仍对其进行修改的同时显示它.

So when something is not in the xml, you can't use onchange on it. It's a real pain if you want to change something in another model or module. But more of that it was really stupid to try this and it perfectly make sense: the purpose of onchange is to update before any registration is done, the only interest is to show it while your still modify it.

借助Odoo 8.0 API,该解决方案非常简单.这是一个示例(这是合作伙伴):

The solution was quite simple thanks to the Odoo 8.0 API. Here is an exemple (This is in res partner):

'my_field': field.many2one(compute='_compute_second_field', store=True, readonly=True)

@api.depends('name_ids.type') #name_ids is the one2many reverse of name's many2one in model1
for record in self:
    if self.name_ids: #if some names are pointing to this, name_ids is not empty
        record.model3_id = 2
    else: #if doesn't exist, default value 
        record.model3_id = 0

因此,当您在res_partner中创建时,此res_partner仍不在model1中,因此取值为'0'.然后,将其添加到模型1中,取值为2.如果将其从模型1中删除,请再次取值"0"(删除仍是修改项).

So when you create in res_partner, this res_partner is still not in model1, take value '0'. Then you add it in model1, take value 2. If you remove him from model1, take value '0' again (removing is still a modification).

对于openerp-7,它是"fields.function"(使用type ='many2one'作为参数,store = {('model1':_ other_function,['type'],10)}},但我没有这样做不能以这种方式完成它,所以我不会显示它

这篇关于Odoo:从many2one访问一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆