コンテンツへスキップ
メニュー
この質問にフラグが付けられました
1 返信
7850 ビュー

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

アバター
破棄
最善の回答

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

アバター
破棄
関連投稿 返信 ビュー 活動
2
1月 25
8573
2
12月 20
2618
0
2月 20
1316
2
10月 19
6749
1
4月 23
7689