There's more...

There is a shortcut for this inheritance delegation. Instead of creating an _inherits dictionary, you can use the delegate=True attribute in the Many2one field definition. This will work exactly like the _inherits option. The main advantage is that this is simpler. In the given example, we have performed the same inheritance delegation as in the previous one, but in this case, instead of creating an _inherits dictionary, we have used the delegate=True option on the partner_id field:

class LibraryMember(models.Model):
_name = 'library.member'
partner_id = fields.Many2one('res.partner', ondelete='cascade', delegate=True)
date_start = fields.Date('Member Since')
date_end = fields.Date('Termination Date')
member_number = fields.Char()
date_of_birth = fields.Date('Date of birth')

A noteworthy case of delegation inheritance is the Users model, res.users. It inherits from Partners (res.partner). This means that some of the fields that you can see on the User are actually stored in the Partner model (notably, the name field). When a new User is created, we also get a new, automatically created Partner.

We should also mention that traditional inheritance with _inherit can also copy features into a new model, although in a less efficient way. This was discussed in the Adding features to a model using inheritance recipe.

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

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