0
votes

I have a model that inherits from "hr.employee" and I have an action that will display only the employees which are in a special department.

I would like that the "Create" button on top of that list allows me to create a record in hr.employee with the department field already set to that special department.

Of course the other "Create" button, from the standard "hr" module, should not set a default value for the department.

Here's the model:

# -*- coding: utf-8 -*-
from openerp import models, fields

class Patient(models.Model):
    _inherit = 'hr.employee'

    date_debut_contrat = fields.Date('Début Contrat de Prise en Charge')
    date_fin_contrat   = fields.Date('Fin Contrat de Prise en Charge')

And here's the action:

<record id="action_liste_patients" model="ir.actions.act_window">
    <field name="name">Patients</field>
    <field name="res_model">hr.employee</field>
    <field name="view_mode">kanban,tree,form</field>
    <field name="domain">[('department_id.is_patients', '=', 'true')]</field>
</record>

Do I have a way to tell Odoo that from that action, the department_id field should have a default value, without changing the behaviour of the standard action ?

Many thanks to all.

Marc

2

2 Answers

2
votes

You can use

<field name="context">{'department_id': 'special department'}</field>

Now while creating Patient, in create() you can fetch value from context and act accordingly.

0
votes

Based on Jamin's comment, here's the full solution I applied:

1) Add a context to the action. The context will have a value named "create_as_patient", which is True or False.

<record id="action_liste_patients" model="ir.actions.act_window">
    <field name="name">Patients</field>
    <field name="res_model">hr.employee</field>
    <field name="view_mode">kanban,tree,form</field>
    <field name="domain">[('department_id.is_patients', '=', 'true')]</field>
    <field name="context">{'create_as_patient': True}</field>
</record>

2) In the create() method of my model, I fetch the value from the context if it's present. Then I search in the "hr.department" model for the department which is marked as default department for patients. Then, if the default department has been found, I set the new record's department_id to it.

# -*- coding: utf-8 -*-
from openerp import models, fields, api

class Patient(models.Model):
    _inherit = 'hr.employee'

    date_debut_contrat = fields.Date('Début Contrat de Prise en Charge')
    date_fin_contrat   = fields.Date('Fin Contrat de Prise en Charge')

    @api.model
    def create(self, vals):
        if 'create_as_patient' in self.env.context:
            if self.env.context['create_as_patient']:
                departmentId = self.env['hr.department'].search([('is_patients', '=', True)], limit=1).id
                if departmentId is not None:
                    vals['department_id'] = departmentId

        record = super(Patient, self).create(vals)
        return record

This meets my requirements :

  • From the Employee action, which doesn't have the 'create_as_patient' flag in its context, newly created employees will not have the default department set.
  • On the other hand, if I create a record from my Patient action, the flag will be present, and the default department will be added to the new record.

Many thanks again to jamin for pointing me the right direction :-)

Marc