Skip to content

5.0 step2 correction

Jean Cavallo edited this page Aug 8, 2019 · 2 revisions

Step 2 - Homework correction

The editor field

You should have one more field on the library.book model. It should be wisely named editor, and be defined right before or after the existing genre field (since it is a data field).

editor = fields.Many2One('library.editor', 'Editor', ondelete='RESTRICT',
    required=True)

ondelete should be to 'RESTRICT' because once an editor edited a book, it will remain as an editor whether he liked it or not.

required should be True since every book is edited by someone. However False could be understandable for ancient books. Later on we will consider it required.

The mystery structural field

The missing relation is that between authors and books.

A given book have one and only one author (there are exceptions to this of course, but for the sake of simplicity we will stick to this).

Note: If you chose to implement a Many2Many relationship, you were not wrong! We decided to use a Many2One because it will be eaiser to use later, and also because some things would be more complicated to manage with Many2Many fields (for instance, making sure that a book has at least one author). However a multi-author model also makes perfect sense, and actually covers more real-life cases

So we add a new Many2One field on the library.book model, right at the top because it is structure:

author = fields.Many2One('library.author', 'Author', required=True,
    ondelete='CASCADE')

The other one

Since author is structural to books, we can (and should) define the corresponding One2Many field:

books = fields.One2Many('library.book', 'author', 'Books')
Clone this wiki locally