How to do it...

To add a view, we will add an XML file with its definition to the module. Since it is a new model, we must also add a menu option for the user to be able to access it.

Be aware that the sequence of the following steps is relevant, since some of them use references to IDs that are defined in the preceding steps:

  1. Create the XML file to add the data records describing the user interface views/library_book.xml:
<?xml version="1.0" encoding="utf-8"?> <odoo> <!-- Data records go here --> </odoo>
  1. Add the new data file to the add-on module manifest, __manifest__.py, by adding it to views/library_book.xml:
{ 
    'name': "My Library", 
    'summary': "Manage books easily", 
    'depends': ['base'], 
    'data': ['views/library_book.xml'], 
} 
  1. Add the action that opens the views in the library_book.xml file:
<record id='library_book_action' model='ir.actions.act_window'>
<field name="name">Library Books</field>
<field name="res_model">library.book</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
</record>
  1. Add the menu items to the library_book.xml file, making it visible to the users:
<menuitem name="My Library" id="library_base_menu" />
<menuitem name="Books" id="library_book_menu" parent="library_base_menu" action="library_book_action"/>

  1. Add a custom form view to the library_book.xml file:
<record id="library_book_view_form" model="ir.ui.view">
<field name="name">Library Book Form</field>
<field name="model">library.book</field>
<field name="arch" type="xml">
<form>
<group>
<group>
<field name="name"/>
<field name="author_ids" widget="many2many_tags"/>
</group>
<group>
<field name="date_release"/>
</group>
</group>
</form>
</field>
</record>
  1. Add a custom tree (list) view to the library_book.xml file:
<record id="library_book_view_tree" model="ir.ui.view"> 
  <field name="name">Library Book List</field> 
  <field name="model">library.book</field> 
  <field name="arch" type="xml"> 
    <tree> 
      <field name="name"/> 
      <field name="date_release"/> 
    </tree> 
  </field> 
</record> 
  1. Add custom Search options to the library_book.xml file:
<record id="library_book_view_search" model="ir.ui.view"> 
  <field name="name">Library Book Search</field> 
  <field name="model">library.book</field> 
  <field name="arch" type="xml"> 
    <search> 
      <field name="name"/> 
      <field name="author_ids"/> 
      <filter string="No Authors"
name="without_author" domain="[('author_ids','=',False)]"/> </search> </field>
</record>

From version 12, admin user must gain proper access rights in order to access our model from the user interface. Odoo will not show your menus and views until you give proper access rights. In the next recipe, we will add access rights to our model, and only after that will you be able to see your menus and views from the admin user.

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

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