odoo的學習步驟五:inhert與xpath

yhwu123發表於2020-11-30

model:

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class test(models.Model):
    _name = 'test.test'

    name = fields.Char(string='名字')
    sex = fields.Char(string='性別')
    age = fields.Char(string='年齡')
    hobby_id = fields.Many2one('test.hobby',ondelete='set null',string='愛好')



class hobby(models.Model):
    _name = 'test.hobby'

    name = fields.Char(string='愛好')
    person_ids = fields.One2many('test.test','hobby_id',string='愛好的人')
    person_ids_num = fields.Integer(string='愛好的人的數量',compute='_compute_person_num')

    @api.depends('person_ids')
    @api.multi
    def _compute_person_num(self):
        for i in self:
            i.person_ids_num = len(i.person_ids)

xml:

<odoo>
  <data>
    <record model="ir.ui.view" id="hobby_list">
      <field name="name">hobby_list</field>
      <field name="model">test.hobby</field>
      <field name="arch" type="xml">
        <tree>
          <field name="name"/>
          <field name="person_ids_num"/>
        </tree>
      </field>
    </record>

    <record model="ir.ui.view" id="hobby_form">
      <field name="name">hobby_form</field>
      <field name="model">test.hobby</field>
      <field name="arch" type="xml">
        <form>
          <group colspan="4">
              <group colspan="2" col="2">
                  <field name="name"/>
              </group>

              <notebook colspan="4">
                  <page string="person">
                      <field name="person_ids">
                          <tree>
                              <field name="name"/>
                              <field name="sex"/>
                              <field name="age"/>
                          </tree>
                      </field>
                  </page>
              </notebook>
          </group>
        </form>
      </field>
    </record>



    <record model="ir.ui.view" id="myself_list">
      <field name="name">myself_list</field>
      <field name="model">test.test</field>
      <field name="arch" type="xml">
        <tree>
          <field name="name"/>
          <field name="sex"/>
          <field name="age"/>
          <field name="hobby_id"></field>
        </tree>
      </field>
    </record>

    <record model="ir.ui.view" id="myself_form">
      <field name="name">myself_form</field>
      <field name="model">test.test</field>
      <field name="arch" type="xml">
        <form>
          <group colspan="4">
              <group colspan="2" col="2">
                  <separator string="Myname" colspan="2"/>
                  <field name="name"/>
                  <!--<field name="age" nolabel="1"/>-->
              </group>

              <group colspan="2" col="2">
                  <separator name="title" string="Mysex" colspan="2"/>
                  <!--<field name="sex"/>-->
              </group>

              <notebook colspan="4">
                  <page string="hobby">
                      <field name="hobby_id" nolabel="1"/>
                  </page>
              </notebook>
          </group>
        </form>
      </field>
    </record>

    <record model="ir.ui.view" id="add_field">
        <field name="name">add_field</field>
        <field name="model">test.test</field>
        <field name="inherit_id" ref="myself_form"></field>
        <field name="arch" type="xml">
            <xpath expr="//field[@name='name']" position="before">
                <field name="age"></field>
            </xpath>
            <xpath expr="//separator[@name='title']" position="after">
                <field name="sex"></field>
            </xpath>

        </field>
    </record>

    <record model="ir.ui.view" id="myself_search">
        <field name="name">myself_search</field>
        <field name="model">test.test</field>
        <field name="arch" type="xml">
            <search>
                <field name="name"></field>
                <field name="sex"></field>
                <field name="age"></field>
                <separator/>
                <filter string="超過" name="more" domain="[('age','>','18')]"></filter>
                <separator/>
                <group>
                    <filter string="sex" name="sex" domain="[]" context="{'group_by':'sex'}"></filter>
                </group>
            </search>
        </field>
    </record>

    <record model="ir.actions.act_window" id="myself_ideas">
      <field name="name">myself</field>
      <field name="res_model">test.test</field>
      <field name="view_model">tree,form</field>
    </record>

    <record model="ir.actions.act_window" id="hobby_idea">
      <field name="name">hobby</field>
      <field name="res_model">test.hobby</field>
      <field name="view_model">tree,form</field>
    </record>


    <menuitem id="first_menu" name="first_menu"/>
    <menuitem id="second_menu" name="second_menu" parent="first_menu"/>
    <menuitem id="menu_myself" name="menu_myself" parent="second_menu" action="myself_ideas"/>
    <menuitem id="menu_hobby" name="menu_hobby" parent="second_menu" action="hobby_idea"/>
  </data>
</odoo>

相關文章