Zum Inhalt springen
Menü
Sie müssen registriert sein, um mit der Community zu interagieren.
Diese Frage wurde gekennzeichnet
1 Antworten
7981 Ansichten

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
Verwerfen
Beste Antwort

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
Verwerfen
Verknüpfte Beiträge Antworten Ansichten Aktivität
2
Jan. 25
8824
2
Dez. 20
2721
0
Feb. 20
1316
2
Okt. 19
6944
1
Apr. 23
7826