找不到相关字段odoo 8的引用ID

时间:2016-03-28 08:06:20

标签: python python-2.7 openerp odoo-8

我试图将many2one字段与另一个many2one字段关联但返回错误。

我在创建方法上使用不起作用,我试过

jounral = field.Many2one('erp.journal', related="journal_item.journal 

工作但是在升级数据库时我收到错误

  

找不到journal.entries的参考字段journal_id。

这是我的代码

class JournalItem(models.Model):
_name = 'journal.item'

name = fields.Char()
journal = fields.Many2one('erp.journal')
entries = fields.One2many('journal.entries', 'journal_item')


class JournalEntries(models.Model):
    _name = 'journal.entries'

    #record Created but didn't work
    @api.model
    def create(self, vals):
        vals['journal_id'] = self.journal_item.journal
        return super(JournalEntries, self).create(vals)

    #Error NoneType object has no attribute 'id'
    @api.model
    def create(self, vals):
        for x in self:
            x.journal_id = x.journal_item.journal
            return super(JournalEntries, self).create(vals)

    journal_item = fields.Many2one('journal.item')
    journal_id = fields.Many2one('erp.journal')

1 个答案:

答案 0 :(得分:0)

首先,您需要了解 @api 装饰器的用法,这些装饰器应该在何时实现。

@ api.model 没有任何记录集列表,因此您无法遍历它。

您可以通过定义关系字段轻松实现此目的。

class JournalEntries(models.Model):
    _name = 'journal.entries'

    journal_item = fields.Many2one('journal.item')
    journal_id = fields.Many2one(comodel_name='erp.journal', related='journal_item.journal', string='Journal', readonly=True)

Click here to read about @api decorators