Search code examples
pythonodoo

How to create an incremental field (sequence) that is validated with a method (def)


i've one doubt:

I created a field called niu to increase its value for each product type stockable.

niu = fields.Char(string="NIU", compute="_niu_validation", defalut=" ", readonly=True)

With the attribute compute=_ niu_validation I call the method of the same name. In this, I want to validate that the product type is stockable type.

@api.depends('product_id.product_tmpl_id.type')
def _niu_validation(self):
    if 'product_id.product_tmpl_id.type' == 'product':
        niu = lambda obj, cr, uid, context: obj.pool.get('ir.sequence').get(cr, uid, 'sale.order.line')
        return super(SaleOrderLine,self)

On the other hand I created the render sequence for ' niu ' field in sale.order.line model.

<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data noupdate="1">
        <!-- Sequence for sale.order.line -->
        <record id="seq_sale_order_line" model="ir.sequence">
            <field name="name">NIU Sequence</field>
            <field name="code">sale.order.line</field>
            <field name="prefix">00</field>
            <field name="padding">3</field>
        </record>
        </data>
</openerp>

And in the view , I want for each product type stockable, the field 'niu' increase its value.

Image: http://en.zimagez.com/zimage/viewsequenceniu.php

Please I need help because I 've been a long time on this and I can't do it on my own. I hope your help , advice , recommendations. Thank you very much to all.


Solution

  • In your code you are assigning a function(lambda) to niu:

    niu = lambda ...
    

    and you return super(), return is not needed.

    To assign a new value to niu field use:

    niu = value
    

    To increment its value you can use:

    sequence = self.env['ir.sequence'].next_by_code('sale.order.line')
    for rec in self:
        rec.niu = sequence
    

    Use odoo official documentation (very useful), go to this LINK and search for Computed fields.

    EDIT: Check if niu is already set (add and not rec.niu to the condition):

    @api.depends('product_id.product_tmpl_id.type')
    def _niu_validation(self):
        ir_sequence = self.env['ir.sequence']
        for rec in self:
            if rec.product_id.product_tmpl_id.type == 'product' and not rec.niu:
                rec.niu = ir_sequence.next_by_code('sale.order.line')