模型拓展
注意
1.不管是继承还是,重写要写全xml-id,加上模块名的完整外部id--要先depand上
2.关于depend哪个模块的问题,视图或者模型在哪里新建的就depend谁!
传统继承
能够添加修改字段,添加约束,添加修改方法
类继承
_name='library.book' #可省略
_inherit = 'library.book'
修改原表不会有新表产生
添加字段
from odoo import fields, models
class Book(models.Model):
_inherit = 'library.book'
is_available = fields.Boolean('Is Available?')
修改字段
class Book(models.Model):
_inherit = 'library.book'
isbn = fields.Char(help="Use a valid ISBN-13 or ISBN-10.")
publisher_id = fields.Many2one(index=True)
原型继承
_name='library.book2'
_inherit = 'library.book'
重新创建了一个新的模型,这个模型里面有模型1的字段和方法,支持多继承_inherit
单继承例子
继承一个_name默认有,继承多个最好指定
class Inheritance0(models.Model):
_name = 'inheritance.0'
name = fields.Char()
def call(self):
return self.check("model 0")
def check(self, s):
return "This is {} record {}".format(s, self.name)
class Inheritance1(models.Model):
_name = 'inheritance.1'
_inherit = 'inheritance.0'
def call(self):
return self.check("model 1")
多继承例子
模型就会包含这些 mixin 的所有字段和方法。
_inherit = ['mail.thread', 'mail.activity.mixin']
mail.thread提供在许多文档表单下方或右侧的消息面板功能,以及消息和通知相关逻辑。这在我们自己的模型中将经常会添加,下面就来一起学习下。
mail.activity.mixin模型提供待办任务计划。
<form>
...
<!-- mail mixin fields -->
<div class="oe_chatter">
<field name="message_follower_ids" widget="mail_followers" />
<field name="activity_ids" widget="mail_activity" />
<field name="message_ids" widget="mail_thread" />
</div>
</form>
委托继承
#相当于两个多对一,可以读写子模型的字段,如果子模型里的字段重复,只能看到_inherits第一个子模型的字段
#不继承方法
class Child0(models.Model):
_name = 'delegation.child0'
field_0 = fields.Integer()
class Child1(models.Model):
_name = 'delegation.child1'
field_1 = fields.Integer()
class Delegating(models.Model):
_name = 'delegation.parent'
_inherits = {
'delegation.child0': 'child0_id',
'delegation.child1': 'child1_id',
}
child0_id = fields.Many2one('delegation.child0', required=True, ondelete='cascade')
child1_id = fields.Many2one('delegation.child1', required=True, ondelete='cascade')
代理继承内嵌模型
这种继承方式,除了本表的字段,还可以使用partner的所有字段
两张表多对一关联,一个联系人有多个会员卡,没有一对一,其实多对一相当于包含一对一
每次创建一个member就会有一个res.partnerchuangjian
from odoo import fields, models
class Member(models.Model):
_name = 'library.member'
_description = 'Library Member'
card_number = fields.Char()
partner_id = fields.Many2one(
'res.partner',
delegate=True,
ondelete='cascade',
required=True)
视图拓展
继承视图
1.继承视图的xml ID
2.定位,然后添加字段
<?xml version="1.0"?>
<odoo>
<record id="view_form_book_extend" model="ir.ui.view">
<field name="name">Book: add Is Available? field</field>
<field name="model">library.book</field>
<field name="inherit_id" ref="library_app.view_form_book" />
<field name="arch" type="xml">
<field name="isbn" position="after">
<field name="is_available" />
</field>
</field>
</record>
</odoo>
修改数据
此案例展示的是修改权限组的名称
<record id="library_group_user" model="res.groups">
<field name="name">User</field>
<field name="category_id" ref="module_library_category"/>
<field name="implied_ids" eval="[(4, ref('base.group_user'))]"/>
</record>
<odoo>
<!-- Modify Group name -->
<record id="library.library_group_user" model="res.groups">
<field name="name">Librarian</field>
</record>
</odoo>
方法拓展
重写方法,也可以通过super调用原方法
@api.multi
def _check_isbn(self):
self.ensure_one()
isbn = self.isbn.replace('-', '')
digits = [int(x) for x in isbn if x.isdigit()]
if len(digits) == 10:
ponderators = [1, 2, 3, 4, 5, 6, 7, 8, 9]
total = sum(a * b for a, b in zip(digits[:9], ponderators))
check = total % 11
return digits[-1] == check
else:
return super()._check_isbn()
继承 Web 控制器和模板
web控制器
from odoo.addons.library_app.controllers.main import Books
#odoo.addons即使addons_path
from odoo import http
from odoo.addons.library_app.controllers.main import Books
class BookExtended(Books):
@http.route()
def list(self, **kwargs):
response = super().list(**kwargs)
if kwargs.get('available'):
Book = http.request.env['library.book']
books = Book.search([('is_available', '=', True)])
response.qcontext['books'] = books
return response
继承 QWeb 模板
<odoo>
<template id="book_list_extended"
name="Extended Book List"
inherit_id="library_app.book_list_template">
<xpath expr="//span[@t-field='book.publisher_id']" position="after">
<t t-if="not book.is_available">
<b>(Not Available)</b>
</t>
</xpath>
</template>
</odoo>