Odoo - How add a field to view from another wizard model?

Viewed 29

I want the use a field from wizard to my form view.

Let me explain with code:

class CancelAppointmentWizard(models.Model):
    _name = "cancel.appointment.wizard"
    _description = "Cancel Appointment Wizard"

    reason = fields.Text(string="Reason")

and i want the show this "reason" field inside of some form views.

<record id="view_hospital_appointment_form" model="ir.ui.view">
        <field name="name">hospital.appointment.form</field>
        <field name="model">hospital.appointment</field>
        <field name="arch" type="xml">
            <form>
                <sheet>
                    <group>
                        <field name="reason"/>
                    </group>
                </sheet>
            </form>
        </field>
    </record> 

but of course this give me error like

hospital.appointment don't have a field like reason.

How can show this field ? I tried to make a dummy code, i hope i was able to explain my problem.

1 Answers

I assume the wizard is called from the view_hospital_appointment_form. And in the wizard view let say there is a button tag with name attribute, which is this attribute associated to python method in your wizard class.

Wizard view

<record id="view_cancel_appointment_wizard_form" model="ir.ui.view">
    <field name="name">cancel.appointment.wizard.form</field>
    <field name="model">cancel.appointment.wizard</field>
    <field name="arch" type="xml">
        <form>
            …
            …
            <footer>
                <button name="action_ok" string="Ok" type="object"/>
            </footer>
        </form>
    </field>
</record>

Python method

def action_ok(self):
    ids = self.env.context.get('active_ids')

    hospital_appointment = self.env['hospital.appointment'].browse(ids)
    hospital_appointment.reason = self.reason

    return {'type': 'ir.actions.act_window_close'}
Related