Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Odoo 12:属性错误:';int';对象没有属性';获取';_Python 3.x_Odoo_Odoo 12 - Fatal编程技术网

Python 3.x Odoo 12:属性错误:';int';对象没有属性';获取';

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

我创建了一个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()  )

@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,使用browse
self.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
,只是为了澄清一下。