{"id":16,"date":"2023-05-10T04:25:00","date_gmt":"2023-05-10T04:25:00","guid":{"rendered":""},"modified":"2024-01-28T09:05:21","modified_gmt":"2024-01-28T09:05:21","slug":"type-of-model-inheritance-in-odoo","status":"publish","type":"post","link":"https:\/\/pythonpower.in\/index.php\/2023\/05\/10\/type-of-model-inheritance-in-odoo\/","title":{"rendered":"Type of model inheritance in odoo"},"content":{"rendered":"<p>&nbsp;<\/p>\n<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/pythonpower.in\/wp-content\/uploads\/2023\/05\/Expert-20In-20Odoo-2016-20-20Hindi.jpg\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" decoding=\"async\" border=\"0\" data-original-height=\"720\" data-original-width=\"1280\" height=\"360\" src=\"http:\/\/pythonpower.in\/wp-content\/uploads\/2023\/05\/Expert-20In-20Odoo-2016-20-20Hindi-300x169.jpg\" width=\"640\" \/><\/a><\/div>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p><\/p>\n<p><span face=\"S\u00f6hne, ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, sans-serif, Helvetica Neue, Arial, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji\" style=\"color: #ececf1; font-size: large;\"><span style=\"background-color: #343541; white-space: pre-wrap;\">Type Of model Inheritance in odoo:<\/span><\/span><\/p>\n<p><span style=\"font-size: large;\">Inheritance by extension:<\/span><\/p>\n<p><\/p>\n<p><\/p>\n<p><span style=\"font-size: medium;\">from odoo import models, fields, api<\/span><\/p>\n<p><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p><span style=\"font-size: medium;\">class ParentModel(models.Model):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; _name = &#8216;parent.model&#8217;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; name = fields.Char()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; age = fields.Integer()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; @api.multi<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; def do_something(self):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; # some functionality<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; pass<\/span><\/p>\n<p><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p><span style=\"font-size: medium;\">class ChildModel(models.Model):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; _name = &#8216;child.model&#8217;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; _inherit = &#8216;parent.model&#8217;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; email = fields.Char()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; @api.multi<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; def do_something(self):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; # overriding parent method<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; pass<\/span><\/p>\n<p><span style=\"font-size: medium;\">In this example, the ChildModel inherits from the ParentModel using the _inherit attribute. The child model also adds a new field email and overrides the do_something method of the parent model.<\/span><\/p>\n<p><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p><span style=\"font-size: medium;\">Inheritance by delegation:<\/span><\/p>\n<p><\/p>\n<p><span style=\"font-size: medium;\">from odoo import models, fields, api<\/span><\/p>\n<p><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p><span style=\"font-size: medium;\">class ParentModel(models.Model):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; _name = &#8216;parent.model&#8217;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; name = fields.Char()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; age = fields.Integer()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; @api.multi<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; def do_something(self):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; # some functionality<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; pass<\/span><\/p>\n<p><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p><span style=\"font-size: medium;\">class ChildModel(models.Model):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; _name = &#8216;child.model&#8217;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; parent_id = fields.Many2one(&#8216;parent.model&#8217;)<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; email = fields.Char()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; @api.multi<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; def do_something(self):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; # some functionality delegated to parent model<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; self.parent_id.do_something()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; pass<\/span><\/p>\n<p><span style=\"font-size: medium;\">In this example, the ChildModel does not inherit from the ParentModel. Instead, it creates a Many2one field parent_id to delegate some of its functionality to the ParentModel. The child model also adds a new field email and overrides the do_something method to delegate some functionality to the parent model.<\/span><\/p>\n<p><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p><span style=\"font-size: medium;\">Inheritance by prototype:<\/span><\/p>\n<p><span style=\"font-size: large;\">from odoo import models, fields, api<\/span><\/p>\n<p><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p><span style=\"font-size: medium;\">class ParentModel(models.Model):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; _name = &#8216;parent.model&#8217;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; name = fields.Char()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; age = fields.Integer()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; @api.multi<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; def do_something(self):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; # some functionality<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; pass<\/span><\/p>\n<p><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p><span style=\"font-size: medium;\">class ChildModel(models.Model):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; _name = &#8216;child.model&#8217;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; _inherits = {&#8216;parent.model&#8217;: &#8216;parent_id&#8217;}<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; parent_id = fields.Many2one(&#8216;parent.model&#8217;)<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; email = fields.Char()<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp;&nbsp;<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; @api.multi<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; def do_something(self):<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; # overriding parent method<\/span><\/p>\n<p><span style=\"font-size: medium;\">&nbsp; &nbsp; &nbsp; &nbsp; pass<\/span><\/p>\n<p><span style=\"font-size: medium;\">In this example, the ChildModel inherits from the ParentModel using the _inherits attribute to create a copy of the ParentModel. The child model also adds a new field email and overrides the do_something method of the parent model.<\/span><\/p>\n<p><span style=\"font-size: medium;\"><br \/><\/span><\/p>\n<p><span style=\"font-family: &quot;Helvetica Neue&quot;;\"><span style=\"font-size: medium;\">For any inquiries, suggestions, or feedback, you can reach out to us through the provided contact form on our blog. We make an effort to respond to all queries in a timely manner and appreciate your engagement with our content.<\/span><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Type Of model Inheritance in odoo: Inheritance by extension: from odoo import models, fields,&hellip;<\/p>\n","protected":false},"author":2,"featured_media":76,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":"","_joinchat":[]},"categories":[5],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/16"}],"collection":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":1,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"predecessor-version":[{"id":77,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/posts\/16\/revisions\/77"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/media\/76"}],"wp:attachment":[{"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonpower.in\/index.php\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}