Passa al contenuto
Menu
È necessario essere registrati per interagire con la community.
La domanda è stata contrassegnata
1 Rispondi
8082 Visualizzazioni

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
Abbandona
Risposta migliore

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
Abbandona
Post correlati Risposte Visualizzazioni Attività
2
gen 25
8944
2
dic 20
2769
0
feb 20
1316
2
ott 19
7062
1
apr 23
7974