Skip to content

Latest commit

 

History

History
60 lines (44 loc) · 1.87 KB

8_createMethod.md

File metadata and controls

60 lines (44 loc) · 1.87 KB

Override Create Method

  • To override the create method we have to add a method named create like below. It will override the description field if it is empty.

    @api.model
    def create(self, vals):
        if not vals['description']:
            vals['description'] = "Enter the description here"
        res = super(HospitalAppointment, self).create(vals)
        return res

    createMethod1

Generate Sequential Value For Field

  • First we have to create a data directory and create a file named appointment_seq.xml. Insert the below code for creating a sequence.

    <record id="seq_appointment" model="ir.sequence">
        <field name="name">Appointment Sequence</field>
        <field name="code">kmhospital.appointment</field>
        <field name="prefix">AS</field>
        <field name="padding">5</field>
        <field name="company_id" eval="False"/>
    </record>
  • Check Settings > Technical > Sequences if the sequence is created or not.

  • Add a field name to the appointment.py file.

    name = fields.Char(string='Appointment Reference', required=True, copy=False, readonly=True, default=lambda self: _('New'))
  • Add this code to the create method.

    if vals.get('name', _('New')) == _('New'):
        vals['name'] = self.env['ir.sequence'].next_by_code('kmhospital.appointment') or _('New')
  • Add the code in the appointment_view.xml file inside the sheet tag.

    <div class="oe_title">
        <h1>
            <field name="name"/>
        </h1>
    </div>

    createMethod2

🚀 Happy Coding ! 🔥