How it works...

The _inherits model attribute sets the parent models that we want to inherit from. In this case, just one—res.partner. Its value is a key-value dictionary where the keys are the inherited models, and the values are the field names used to link to them. These are Many2one fields that we must also define in the model. In our example, partner_id is the field that will be used to link with the Partner parent model.

To better understand how it works, let's look at what happens on the database level when we create a new Member:

  • A new record is created in the res_partner table
  • A new record is created in the library_member table
  • The partner_id field of the library_member table is set to the id of the
    res_partner record that is created for it

The Member record is automatically linked to a new Partner record. It's just a many-to-one relation, but the delegation mechanism adds some magic so that the Partner's fields are seen as if belonging to the Member record, and a new Partner record is also automatically created with the new Member.

You might like to know that this automatically created Partner record has nothing special about it. It's a regular Partner, and if you browse the Partner model, you will be able to find that record (without the additional Member data, of course). All Members are at the same time Partners, but only some Partners are also Members.

So, what happens if you delete a Partner record that is also a Member? You decide by choosing the ondelete value for the relation field. For partner_id, we used cascade. This means that deleting the Partner will also delete the corresponding Member. We could have used the more conservative setting restrict to forbid deleting the Partner while it has a linked Member. In this case, only deleting the Member will work.

It's important to note that delegation inheritance only works for fields and not for methods. So, if the Partner model has a do_something() method, the Members model will not automatically inherit it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.128.78.30