Python 3.x Odoo 12:属性错误:';int';对象没有属性';获取';
我创建了一个choice\u student方法,在该方法中,我在与res.users相关的manyOne字段的默认值中返回一个ID,但我得到了错误: AttributeError:“int”对象没有属性“get” 这是我的代码:Python 3.x Odoo 12:属性错误:';int';对象没有属性';获取';,python-3.x,odoo,odoo-12,Python 3.x,Odoo,Odoo 12,我创建了一个choice\u student方法,在该方法中,我在与res.users相关的manyOne字段的默认值中返回一个ID,但我得到了错误: AttributeError:“int”对象没有属性“get” 这是我的代码: student_id = fields.Many2one('res.users', 'Etudiant', readonly=True, required=True, default=lambda self: self.choice_student() ) @ap
student_id = fields.Many2one('res.users', 'Etudiant', readonly=True, required=True, default=lambda self: self.choice_student() )
@api.onchange('projet_terminer_id')
def choice_student(self):
return self.env['res.users'].sudo().search([ ('id','=', 45)]).id
将“onchange”装饰符替换为“model” 还包括字段声明 尝试以下代码
student_id = fields.Many2one('res.users', 'Etudiant', readonly=True,
required=True, default=_default_choice_student)
@api.model
def _default_choice_student(self):
return self.env['res.users'].sudo().search([('id','=', 45)]).id
为了解释为什么会出现此错误,让我们一步一步地开始: 1-对于默认值,请使用decorator
api.model
,如果您有id,请不要使用search,使用browseself.env['some.model'].browse(id)
,但如果您根本不需要它,只需执行以下操作:
student_id = fields.Many2one('res.users', 'Etudiant',
readonly=True, required=True,
default=45 )
2-onchange
也是一种设置默认值的方法,但仅在视图上设置默认值,因为它们是加载默认值后客户端首先触发的,所以onchange方法应返回None
或dictionary
,这就是为什么会出现错误,AttributeError:“int”对象没有属性“get”
,因为返回的值不是None
,所以odoo试图从字典中获取一些期望值(例如:域
,值
),但是糟糕的是,您返回的int不是字典,这就是odoo抛出此错误的原因
在onchage方法中,只需直接在self
记录上设置值:
student_id = fields.Many2one('res.users', 'Etudiant',
readonly=True, required=True,
default=45)
@api.onchange('projet_terminer_id')
def choice_student(self):
self.student_id = self.env['res.users'].sudo().browse(45)
从代码的外观来看,如果您想在更改
projet\u-terminer\u-id
字段时重置student\u-id
的值,我认为您可以保留onchange
。他的方法的返回值是int
而不是None
,只是为了澄清一下。