OpenERP继承与自定义模块

时间:2014-01-18 21:35:35

标签: openerp

我创建了一个自己的模块 Test_Module

此外,我已继承 hr_holidays 类并查看 Test_Module

对于继承视图,我添加了新字段 Employee_Name 。根据设计,此字段会从我的自定义模块中提取所有员工姓名。

然而,继承的视图也有Employee字段,这是继承的一部分。

有没有办法删除这个字段,员工?。我不想使用这个字段的员工,而是想使用我添加的字段 employee_name

事实上,我尝试删除继承的字段员工,但没有运气。系统抛出错误,指出test.base中不存在user_id。我进一步检查了user_id用于 hr_holidays 类方法。我想知道专家的解决方案。谢谢enter image description here

.py文件

rom osv import osv
from osv import fields

class test_base(osv.osv):
    _name='test.base'

    _columns={
        'name':fields.char('Name'),
        'email':fields.char('Email'),
        'code':fields.char('code'),
        'sal':fields.float('Salary'),
        'rate':fields.selection([(10,'10'),(20,'20'),(30,'30')],'rate'),
        'ded':fields.float('ded'),
        'ns':fields.float('Net Salary'),
        'skillid':fields.one2many('test.performance','exp','Skill'), 
        'emphistory':fields.many2many('best.base','test_best_rel','emphistory','pc','Employee history'),


    }
def rate_change(self, cr, uid, ids,rate,sal, context=None):

    x = 0.0
    ns=sal
    if rate:

        x=rate*sal
        x=x/100
        ns=sal-x
    return {'value':{'ded':x,'ns':ns}}




test_base()





class test_plus(osv.osv):
    _name='test.plus'

    _columns={
    'name':fields.many2one('test.base','Name'),
    'bonus':fields.float('bonus'),
    'salary':fields.float('Salary'),
    'totalsal':fields.float('Total Salary'),


    }
def name_change(self, cr, uid, ids,name, context=None):


    cr.execute('select sal from test_base where id=%s', (name,))
    salary= cr.fetchone()[0]
    return {'value': {'salary':salary}}
def get_sum(self, cr, uid, ids,context=None):
    sum = 0.0
    for data in self.browse(cr, uid, ids, context=context):
       sum += data.salary + data.bonus
    self.write(cr, uid, ids, {'totalsal':sum})
    return True

test_plus() 


class test_performance(osv.osv):
    _name='test.performance'

    _columns={

        'tech':fields.char('Technology'),
        'exp':fields.integer('Experience'),


         }       
test_performance()


class hr_holidays(osv.osv):
    _name='hr.holidays'
    _inherit="hr.holidays"

    _columns={

    'employee_id':fields.many2one('test.base','Employee Name'),


    }       
hr_holidays()

xml文件

<?xml version="1.0" encoding="utf-8"?>
 <openerp>
 <data>

<record model="ir.ui.view" id="edit_holiday_new_inheirt">
<field name="name">hr.holidays.form</field>
<field name="model">hr.holidays</field>
<field name="type">form</field>
<field name="inherit_id" ref="hr_holidays.edit_holiday_new"/>
<field name="arch" type="xml">
    <field name="name" position="after">
<field name="employee_name" />

</field>
</field>
</record>
<record model="ir.ui.view" id="edit_holiday_new_inheirt">
<field name="name">hr.holidays.tree</field>
<field name="model">hr.holidays</field>
<field name="type">tree</field>
<field name="inherit_id" ref="hr_holidays.edit_holiday_new"/>
<field name="arch" type="xml">
<field name="name" position="after">
<field name="employee_name" />
</field>

</field>
</record>

<record model="ir.actions.act_window" id="action_test_seq44">
        <field name="name">hr holidays</field>
        <field name="res_model">hr.holidays</field>
        <field name="view_type">form</field>
        <field name="view_mode">form,tree</field>
</record>



        <record model="ir.ui.view" id="test_performance_tree">
           <field name="name">test.performance.form</field>
           <field name="model">test.performance</field>
           <field name="type">form</field>
           <field name="arch" type="xml">
               <form string="Test performance">

        <field name="tech"/>
        <field name="exp"/>
               </form>
          </field>
    </record>

    <record model="ir.ui.view" id="test_performance_tree">
       <field name="name">test.performance.tree</field>
           <field name="model">test.performance</field>
           <field name="type">tree</field>
           <field name="arch" type="xml">
               <tree string="Test performance">
                 <field name="tech"/>
                <field name="exp"/>
               </tree>
          </field>
    </record>



   <record model="ir.ui.view" id="test_base_form">
       <field name="name">test.base.form</field>
           <field name="model">test.base</field>
           <field name="type">form</field>
           <field name="arch" type="xml">
               <form string="Test Base">
                   <field name="name"/>
                  <field name="email"/>
                  <field name="code"/>
                  <field name="sal"/>
                  <field name="rate" on_change="rate_change(rate,sal)"/>
                  <field name="ded"  /> 
                  <field name="ns"/> 
                  <newline />
                  <notebook colspan="4">
                  <page string="Employee Skills">
                  <field name="skillid" nolabel="1"/>
                   </page>
                  <page string="Employement History">
                  <field name="emphistory" nolabel="1"/>
                  </page>
                   </notebook>

                 </form>
          </field>
    </record>
          <record model="ir.ui.view" id="test_base_tree">
        <field name="name">test.base.tree</field>
        <field name="model">test.base</field>
        <field name="type">tree</field>
        <field name="arch" type="xml">
            <tree string="Test Base">
                <field name="name"/>
                  <field name="email"/>
                  <field name="code"/>
                  <field name="sal"/>
                  <field name="ns"/> 

            </tree>
        </field>
    </record>

      <record model="ir.ui.view" id="test_plus_form">
       <field name="name">test.plus.form</field>
           <field name="model">test.plus</field>
           <field name="type">form</field>
           <field name="arch" type="xml">
               <form string="Test Plus">
                    <field name="name" on_change="name_change(name)" />
                   <field name="bonus"/> 
                   <field name="salary"/> 
                   <field name="totalsal"/>
                 <button name="get_sum" string="Sum" type="object"/>
                 <button name="318" string="shri" type="action"/>

               </form>
          </field>
    </record>
          <record model="ir.ui.view" id="test_plus_tree">
        <field name="name">test.plus.tree</field>
        <field name="model">test.plus</field>
        <field name="type">tree</field>
        <field name="arch" type="xml">
            <tree string="Test Plus">
              <field name="name"/>
                <field name="bonus"/>

            </tree>
        </field>
    </record>
      <record model="ir.actions.act_window" id="action_test_seq">
        <field name="name">Test Base</field>
        <field name="res_model">test.base</field>
        <field name="view_type">form</field>
        <field name="view_mode">form,tree</field>
    </record>

    <record model="ir.actions.act_window" id="action_test_seq1">
        <field name="name">Test Plus</field>
        <field name="res_model">test.plus</field>
        <field name="view_type">form</field>
        <field name="view_mode">form,tree</field>
    </record>
    <record model="ir.actions.act_window" id="action_test_seq2">
        <field name="name">Test Performance</field>
        <field name="res_model">test.performance</field>
        <field name="view_type">form</field>
        <field name="view_mode">form,tree</field>
    </record>

     <menuitem id="menu_test_base_main" name="Test Base">
    </menuitem>
     <menuitem id="menu_test_base_sub" parent="menu_test_base_main" name="Employee">
    </menuitem>
     <menuitem id="menu_test_sal" parent="menu_test_base_sub"
              name="Employee Salary " action="action_test_seq"/>

    <menuitem id="menu_test_bonus" parent="menu_test_base_sub"
              name="Bonus Structure" action="action_test_seq1"/>
              <menuitem id="menu_test_skill" parent="menu_test_base_sub"
              name="Employee Skills" action="action_test_seq2"/>
              <menuitem id="menu_test_hr" parent="menu_test_base_sub"
              name="Employee leave" action="action_test_seq44"/>

       </data>
</openerp>

2 个答案:

答案 0 :(得分:1)

我能够解决这个问题。

以下是解决方案:

添加了一个继承了hr.employee

的类
class hr_employee(osv.osv):
    _name='hr.employee'
    _inherit='hr.employee'
    _columns={




  }   
hr_employee() 

还在test.base中进行了以下更改

class test_base(osv.osv):
_name='test.base'

_columns={
    'name':fields.many2one('hr.employee','employee_id'),
    'email':fields.char('Email'),
    'code':fields.char('code'),
    'sal':fields.float('Salary'),
    'rate':fields.selection([(10,'10'),(20,'20'),(30,'30')],'rate'),
    'ded':fields.float('ded'),
    'ns':fields.float('Net Salary'),
    'skillid':fields.one2many('test.performance','exp','Skill'),
    'emphistory':fields.many2many('best.base','test_best_rel','emphistory','pc','Employee history'),
    'department_id':fields.integer('Department'),

    }


 class hr_holidays(osv.osv):
    _name='hr.holidays'
    _inherit="hr.holidays"

    _columns={




     }       
    hr_holidays()

答案 1 :(得分:0)

在您从hr_holidays.edit_holiday_new视图继承的XML文件的视图定义中,您添加了一个新字段:

<field name="name" position="after">
    <field name="employee_name" />
</field>

这里需要的神奇词是replace。当您指定放置新字段的位置(employee_name在您的案例中)时,您可以将其放在旧字段的位置:

<field name="name" position="replace">
    <field name="employee_name" />
</field>