Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie
1 Odpowiedz
7837 Widoki

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

Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
2
sty 25
8564
2
gru 20
2599
0
lut 20
1316
2
paź 19
6720
1
kwi 23
7678