如何在odoo-8中使用相关字段(fields.related)?

时间:2021-09-26 22:57:50

I am trying to retrieve comment field(customer internal notes) from res_partner to account invoice module.Right now I just want to print it later I will include it in xml code. I tried in three ways like this,

我正在尝试从res_partner到帐户发票模块检索注释字段(客户内部注释)。现在我只想打印它,我将它包含在xml代码中。我试过三种这样的方式,

1)comment2 = fields.Char(string='Comment',related='res_partner.comment',compute='_compute_com')
@api.multi
def _compute_com(self):
    print self.comment2

2)comment = fields.Many2one('res.partner','Comment',compute='_compute_com')
  @api.multi
  def _compute_com(self):
    print self.comment

3)partner_comment = fields.Char(compute='_compute_com')
 @api.multi
 def _compute_com(self):
    Comment = self.env['res.partner'].browse(partner_id).comment
    print Comment

5 个解决方案

#1


15  

You should use a related field instead:

您应该使用相关字段:

comment = fields.Char(related='partner_id.comment')

If you need to store it in your account_invoice record you also need to add the parameter store=True Problem is, this way you can't just print it but if you need to show it you need to put it into your view.

如果你需要将它存储在account_invoice记录中,你还需要添加参数store = True问题是,这样你不能只打印它,但如果你需要显示它,你需要把它放到你的视图中。

If you really need to print it temporarly you need to do this other way:

如果你真的需要临时打印它,你需要以其他方式做到这一点:

comment = fields.Char(compute='_compute_comment')

def _compute_comment(self):
    for record in self:
        record.comment = partner_id.comment
        print record.comment

#2


2  

Related Field

相关领域

There is not anymore fields.related fields.

没有field.related字段。

Instead you just set the name argument related to your model:

相反,您只需设置与您的模型相关的名称参数:

participant_nick = fields.Char(string='Nick name',
                           related='partner_id.name')

The type kwarg is not needed anymore.

不再需要类型kwarg了。

Setting the store kwarg will automatically store the value in database. With new API the value of the related field will be automatically updated, sweet.

设置商店kwarg将自动将值存储在数据库中。使用新API,相关字段的值将自动更新,甜蜜。

participant_nick = fields.Char(string='Nick name',
                           store=True,
                           related='partner_id.name')

Note

注意

When updating any related field not all translations of related field are translated if field is stored!!

更新任何相关字段时,如果存储字段,则不会翻译相关字段的所有翻译!

Chained related fields modification will trigger invalidation of the cache for all elements of the chain.

链接相关字段修改将触发链的所有元素的高速缓存失效。

#3


1  

in odoo8

在odoo8

if need same object fields to related then you can use related="related field name " use store=True

如果需要相同的对象字段相关,那么你可以使用related =“相关字段名称”使用store = True

comment2 = fields.Char(string='comment',related='comment', store=True)

LINK

链接

#4


0  

Be careful, you have to use the same kind of field !!

小心,你必须使用相同的领域!

(I had the problem with Selection and Char ... so I have to use Selection with Selection)

(我有选择和字符的问题......所以我必须使用Selection with Selection)

#5


-2  

have a problem with my related field , i have two classes : nomenclature and projet_ligne i want to get the value of ' sous' on 'eta' so there s my code

我的相关领域有问题,我有两个类:命名法和projet_ligne我想在'eta'上得到'sous'的值,所以我的代码

class nomenclature(models.Model):
_name = 'nomenclature'

name = fields.Char('Nom de la nomenclature',required=True)
quantite = fields.Integer('Quantité',required=True)   
produit=fields.Many2one('product.product')
sous= fields.Boolean('sous')

class projet_ligne(models.Model):
_name = 'projet.ligne'
#name = fields.Char('nom du sous essaie',required=True)
nomenclature=fields.Many2one('nomenclature',required=True)
responsable=fields.Many2one('res.users',)
projet = fields.Many2one('projet',required=True)  
date= fields.Date()
etat=fields.Boolean('Achevé?')  
reference= fields.Char('Réference')
nature= fields.Char('Nature')
dateprelevement= fields.Date()
lieuprelevement= fields.Char('lieu')
etatvalider= fields.Boolean('Validé')
eta= fields.Boolean(related='nomenclature.sous')

It doesn t work :/

它不起作用:/

#1


15  

You should use a related field instead:

您应该使用相关字段:

comment = fields.Char(related='partner_id.comment')

If you need to store it in your account_invoice record you also need to add the parameter store=True Problem is, this way you can't just print it but if you need to show it you need to put it into your view.

如果你需要将它存储在account_invoice记录中,你还需要添加参数store = True问题是,这样你不能只打印它,但如果你需要显示它,你需要把它放到你的视图中。

If you really need to print it temporarly you need to do this other way:

如果你真的需要临时打印它,你需要以其他方式做到这一点:

comment = fields.Char(compute='_compute_comment')

def _compute_comment(self):
    for record in self:
        record.comment = partner_id.comment
        print record.comment

#2


2  

Related Field

相关领域

There is not anymore fields.related fields.

没有field.related字段。

Instead you just set the name argument related to your model:

相反,您只需设置与您的模型相关的名称参数:

participant_nick = fields.Char(string='Nick name',
                           related='partner_id.name')

The type kwarg is not needed anymore.

不再需要类型kwarg了。

Setting the store kwarg will automatically store the value in database. With new API the value of the related field will be automatically updated, sweet.

设置商店kwarg将自动将值存储在数据库中。使用新API,相关字段的值将自动更新,甜蜜。

participant_nick = fields.Char(string='Nick name',
                           store=True,
                           related='partner_id.name')

Note

注意

When updating any related field not all translations of related field are translated if field is stored!!

更新任何相关字段时,如果存储字段,则不会翻译相关字段的所有翻译!

Chained related fields modification will trigger invalidation of the cache for all elements of the chain.

链接相关字段修改将触发链的所有元素的高速缓存失效。

#3


1  

in odoo8

在odoo8

if need same object fields to related then you can use related="related field name " use store=True

如果需要相同的对象字段相关,那么你可以使用related =“相关字段名称”使用store = True

comment2 = fields.Char(string='comment',related='comment', store=True)

LINK

链接

#4


0  

Be careful, you have to use the same kind of field !!

小心,你必须使用相同的领域!

(I had the problem with Selection and Char ... so I have to use Selection with Selection)

(我有选择和字符的问题......所以我必须使用Selection with Selection)

#5


-2  

have a problem with my related field , i have two classes : nomenclature and projet_ligne i want to get the value of ' sous' on 'eta' so there s my code

我的相关领域有问题,我有两个类:命名法和projet_ligne我想在'eta'上得到'sous'的值,所以我的代码

class nomenclature(models.Model):
_name = 'nomenclature'

name = fields.Char('Nom de la nomenclature',required=True)
quantite = fields.Integer('Quantité',required=True)   
produit=fields.Many2one('product.product')
sous= fields.Boolean('sous')

class projet_ligne(models.Model):
_name = 'projet.ligne'
#name = fields.Char('nom du sous essaie',required=True)
nomenclature=fields.Many2one('nomenclature',required=True)
responsable=fields.Many2one('res.users',)
projet = fields.Many2one('projet',required=True)  
date= fields.Date()
etat=fields.Boolean('Achevé?')  
reference= fields.Char('Réference')
nature= fields.Char('Nature')
dateprelevement= fields.Date()
lieuprelevement= fields.Char('lieu')
etatvalider= fields.Boolean('Validé')
eta= fields.Boolean(related='nomenclature.sous')

It doesn t work :/

它不起作用:/