日安 我有一个关于Odoo 15.0的伙伴的问题,希望你能帮助我. 我正在创建一个模型学生,我想继承像名字,电话和邮箱领域从res partner在我的新模型.但是我遇到了几个问题,我见过几个人成功地继承了其他模块,但只是添加了一个字段或类似的东西.但我有各种各样的问题.我不知道做这件事的正确方法是什么.
我试着直接继承它,首先这样做:
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 fields www.example.com_ids和www.example.com_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 existe la columba « signup_token » en la relación « res_partner »(dunno why突然在西班牙语)暗示该列signup_toke已经存在于res_partner关系中.
我gave up,试着直接使用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>
但是现在我遇到了视图的麻烦,我甚至不知道我是否正确地导入了我需要的字段.拜托,这东西快把我逼疯了!