美好的一天。 我有一个关于 Odoo 15.0 中 res.partner 的问题,希望你能帮助我。 我正在创建一个模型 Student,我想在新模型中继承 res 合作伙伴的姓名、电话和电子邮件等字段。但我遇到了一些麻烦。我见过几个人成功地继承了其他模块,但只是添加了一个字段或类似的东西。但我遇到了各种各样的问题。我不知道正确的做法是什么。
我尝试先这样做直接继承它:
class Student(models.Model):
_name = 'student'
_inherit = 'res.partner'
_description = 'Model to manage school students'
registration_ids = fields.One2many('registration', 'student_id', string='Registration')
student_DNI = fields.Integer(string='ID')
course_id = fields.Many2one('course', string='Course')
birth_date = fields.Date(string='Birth Date')
age = fields.Integer(string='Age', compute='_compute_age', store=True)`
@api.depends('birth_date') def _compute_age(self):
for student in self:
if student.birth_date:
today = fields.Date.today()
student.age = today.year - student.birth_date.year
else:
student.age = 0
我收到此错误: “TypeError:Many2many字段student.channel_ids和res.partner.channel_ids使用相同的表和列”所以我必须单独定义student_channel_ids并将其添加到Student模型中:
student_channel_ids = fields.Many2many( 'mail.channel', relation='student_mail_channel_rel', column1='student_id', column2='channel_id', string='Channels')
然后它向我显示了这条消息: psycopg2.errors.DuplicateColumn: ya exite la columna «signup_token» en la relación «res_partner» (不知道为什么突然用西班牙语)暗示列signup_toke已经存在于res_partner关系中。
我放弃了并尝试直接使用res.partner,如下所示:
partner_id = fields.Many2one('res.partner', string='Partner', required=True, ondelete="cascade")
事情就这样结束了:
class Student(models.Model):
_name = 'student'
_description = 'Model to manage school students'
partner_id = fields.Many2one('res.partner', string='Partner', required=True, ondelete="cascade")
registration_ids = fields.One2many('registration', 'student_id', string='Registration')
student_DNI = fields.Integer(string='ID')
course_id = fields.Many2one('course', string='Course')
birth_date = fields.Date(string='Birth Date')
age = fields.Integer(string='Age', compute='_compute_age', store=True)`
@api.depends('birth_date') def _compute_age(self):
for student in self:
if student.birth_date:
today = fields.Date.today()
student.age = today.year - student.birth_date.year
else:
student.age = 0
这是视图:
<odoo>
<data>
<record model="ir.ui.view" id="view_student_form">
<field name="name">student.form</field>
<field name="model">student</field>
<field name="inherit_id" ref="base.view_partner_form"/>
<field name="arch" type="xml">
<form>
<xpath expr="//field[@name='email']" position="after">
<field name="birth_date"/>
<field name="age"/>
<field name="student_DNI"/>
<field name="course_id"/>
</xpath>
</form>
</field>
</record>
</data>
</odoo>
但现在我在视图方面遇到了麻烦,我什至不知道我是否正确导入了我需要的字段。拜托,这件事让我发疯!
您可以使用“委托继承”来代替。一个很好的例子是模型
res.users
:
class Users(models.Model):
""" User class."""
_name = "res.users"
_description = 'Users'
_inherits = {'res.partner': 'partner_id'}
partner_id = fields.Many2one('res.partner', required=True, ondelete='restrict', auto_join=True, index=True,
string='Related Partner', help='Partner-related data of the user')
所以你的学生班级将如下所示:
class Student(models.Model):
""" Student class."""
_name = "student"
_description = "Studends"
_inherits = {'res.partner': 'partner_id'}
partner_id = fields.Many2one('res.partner', required=True, ondelete='restrict', auto_join=True, index=True,
string='Related Partner', help='Partner-related data of the student')
registration_ids = fields.One2many('registration', 'student_id', string='Registration')
student_DNI = fields.Integer(string='ID')
course_id = fields.Many2one('course', string='Course')
birth_date = fields.Date(string='Birth Date')
age = fields.Integer(string='Age', compute='_compute_age', store=True)