Skip to Content
Menu
This question has been flagged
1 Reply
2663 Views

hello i'm actually encountering a problem, I have followed the tutorial as shown in this link \https://www.youtube.com/watch?v=qyRhjyp1MeE and there is no error in code, but when i'm creating a new employee it doesn't generate a reference number. Reference number remains 'New' in tree view and form view for the created employee. Can anyone help me please?


hr.py :

    name_seq = fields.Char(string='Order Reference', required=True, copy=False, readonly=True, index=True, default=lambda self: _('New'))

    @api.model

    def create(self, vals):

        if vals.get('name_seq', _('New')) == _('New'):

            vals['name_seq'] = self.env['ir.sequence'].next_by_code('hr.employee.sequence', sequence_date=seq_date) or _('New')

        result = super(HrEmployeePrivate, self).create(vals)

        return result

        

sequence.xml:


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

<odoo>

    <data noupdate="1">


        <!-- Sequences for transfer.order -->

        <record id="seq_hr_employee" model="ir.sequence">

            <field name="name">Employee Sequence</field>

            <field name="code">hr.employee.sequence</field>

            <field name="prefix">EMP</field>

            <field name="padding">3</field>

            <field name="company_id" eval="False"/>

        </record>


    </data>

</odoo>


hr_views.xml :


                        <div class="oe_title">

                            <h1>

                                <field name="name_seq" readonly ="1"/>

                            </h1>

                            <h2>

                                <field name="name" placeholder="Employee's Name" required="True"/>

                            </h2>

                            <h3>

                                <field name="job_title" placeholder="Job Position" />

                            </h3>

                            <field name="category_ids" widget="many2many_tags" options="{'color_field': 'color', 'no_create_edit': True}" placeholder="Tags"  groups="hr.group_hr_manager"/>

Avatar
Discard
Best Answer

Hi,

You can see a lot of examples in the existing odoo code itself, if you check the sale module, you can see how the sequence is generated there, in the python you have to add a field first, like this,

name = fields.Char(string='Order Reference', required=True, copy=False, readonly=True, states={'draft': [('readonly', False)]}, index=True, default=lambda self: _('New'))


Then you have to create a sequence in XML,

<record id="seq_sale_order" model="ir.sequence">
<field name="name">Sales Order</field>
<field name="code">sale.order</field>
<field name="prefix">SO</field>
<field name="padding">3</field>
<field name="company_id" eval="False"/>
</record>


Here the giving code is important we will refer the code while overiding the create method and generate the sequence. Now you have to super the create function of corresponding model,

@api.model
def create(self, vals):
if vals.get('name', _('New')) == _('New'):
if 'company_id' in vals:
vals['name'] = self.env['ir.sequence'].with_context(force_company=vals['company_id']).next_by_code('sale.order') or _('New')
else:
vals['name'] = self.env['ir.sequence'].next_by_code('sale.order') or _('New')
result = super(SaleOrder, self).create(vals)
return result

Also you can add the field to view like this,

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


Thanks

Avatar
Discard
Author

thanks for the reply , but it still showing "NEW" for every record which i create in that module