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

Hi, guys
I'm a beginner of odoo. here has a problem thwarted me.

    def _generate(self):
    # prefix WL + yymmdd
    _serial = 'WL' + dt.today().strftime("%y%m%d")

    # search today's last one data on db
    obj = self.env['yc.weight'].search([('name', '=like', _serial + "%")], limit=1, order='name DESC')
    if obj:  
        _next = int(obj[0].name[8:]) + 1
        _serial += '%03d' % _next
    else:
        _serial += '001'
    self.name = _serial


throw error: _next = int(obj[0].name[8:]) + 1 TypeError: 'bool' object is not subscriptable

but it works in Terminal

>>> obj[0].name
'WL190305058'

>>> _next = int(obj[0].name[8:]) + 1

>>> _next
59

I know ir.sequence model can solve this problem, but I still want to know how come.

Thanks

Avatar
Discard
Best Answer

Hi,

Here in your code, one of the variable is getting the value as False, that is why you are getting this error message, so add some print statements in the code and see which one is causing the problem, and rectify it by giving the IF condition.

If you are looking to create sequence for your model, better to follow the standard way of using the ir.sequence, see the sample below,

In XML,

<odoo>
    <data noupdate="1">

        <!-- ------*.xml file------- -->
        <record id="seq_hr_employee" model="ir.sequence">
            <field name="name">omega_sequencer</field>
            <field name="code">hr.employee</field>
            <field name="prefix">QO</field>
            <field name="padding">5</field>
        </record>

    </data>
</odoo>

Then in the Python,

# ------------*.py file------------------
class omega(models.Model):
    _name = 'omega.model'
    _description = 'No Description for now !!'

    sequence_id = fields.Char('Sequence', readonly=True)

    @api.model
    def create(self, vals):
        seq = self.env['ir.sequence'].next_by_code('hr.employee') or '/'
        vals['sequence_id'] = seq
        return super(omega, self).create(vals)


See the link: Create a sequence number

Thanks

Avatar
Discard
Related Posts Replies Views Activity
1
Mar 21
7632
2
Dec 20
2074
0
Feb 20
1316
2
Oct 19
5802
1
Apr 23
6648