Question

I have an application module that have loaded employees for database initialization with custom data in a previous module release:

<?xml version="1.0"?>
<openerp>
    <data>
        ....
        <!-- John Smith -->
        <record id="emp_john_smith" model="hr.employee">
            <field name="name">John Smith</field>
            <field name="company_id" ref="base.main_company"/>
            <field name="department_id" ref="dp_production"/>
            <field name="job_id" ref="jb_production_officer"/>
            <field name="work_email">john@company.com</field>
            <field name="begin_date">2012-01-01</field>
            <field name="gender">male</field>
            <field name="work_location">Madrid</field>
            <field name="lang">es_ES</field>            
        </record>
    ....
    <record id="ctr_john_smith_hr" model="hr.contract">
        <field name="name">John Smith Production Contract</field>
        <field name="employee_id" ref="emp_john_smith"/>
        <field name="job_id" ref="jb_production_officer"/>
        <field name="email">john@company.com</field>
        <field name="date_start">2012-01-01</field>
        <field name="wage">0</field>
        <field name="percent_working_hours">15</field>
        <field name="working_hours" ref="spain_calendar"/>
    </record>
    </data>
</openerp>

But these records must be removed on the next release of that module. Which is the record element i should use on the XML data file of the next release to erase those records?

Was it helpful?

Solution

You can use the delete tag in xml. Dont remove the xml data records you have created in the xml file. In the new version just add the delete tag at the end of the file.

<delete id="module_name.xml_record_id" model="hr.employee"/>

or you can use the function tag as Yucer said

OTHER TIPS

This code should work:

<?xml version="1.0"?>
<openerp>
    ....
    <data>
        <!-- removes John Smith-->
        <function model="hr.contract" name="unlink">
            <function eval="[[('employee_id', '=', ref('emp_john_smith'))]]" model="hr.contract" name="search"/>
        </function>
        <function model="hr.employee" name="unlink">
            <!-- ids = -->   <value eval="[ref('emp_john_smith')]"/>
        </function>
    </data>
</openerp>

But its a little risky remove an hr.employee because it has a lot of related entities. Maybe is better "deactivate" them like this:

<?xml version="1.0"?>
<openerp>
    ....
    <data noupdate="1">
        <!-- deactivates John Smith -->
        <function model="hr.employee" name="deactivate">
            <!-- ids = -->   <value eval="[ref('emp_john_smith')]"/>
        </function>
    </data> 
</openerp>

where deactivate is a method like this:

class hr_employee(Model):
    _name = str('hr.employee')
    _inherit = str('hr.employee')

    def deactivate(self, cr, uid, ids, context=None):
        if isinstance(ids, (int, long)):
            ids = [ids]
        vals = {'active': False}
        res = super(hr_employee, self).write(cr, uid, ids, vals, context)
        return res
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top