In the first step, we have added website.multi.mixin. This mixin adds a basic utility to handle multiple websites in the model. This mixin adds the website_id field in the model. This field is used to determine which website a record is meant for.
In step 2, we have added the website_id field in the form view of the book, so the books will be filtered based on the website.
In step 3, we have modified the domain used to find a list of books. request.website.website_domain() will return the domain that filters out the books that are not from the website.
Notice that there are records that do not have any website_id set. Such records will be displayed on all websites. This means that if you don't have a website_id field on a particular book, then that book will be displayed on all websites.
Then, we added the domain in the web search, as follows:
- In step 4, we have restricted book access. If the book is not meant for the current website, then we will raise a not found error. The method can_access_from_current_website() will return the value True if a book record is meant for the currently active website, and False if a book record is meant for another website.
- If you checked, we have added **post in both controllers. This is because without it, **post /books and /books/<model:library.book:book> will not accept a query parameter. It will also generate an error while switching the website from the website switcher, so we added it. Normally, it is a good practice to add **post in every controller so that it can handle query parameters.