Skip to Content
Menu
This question has been flagged
1 Reply
5597 Views

What is wrong with the following code? I set default value preset_quantity as 1.0, but in the view it appears 0.0 instead. 

I'm using Odoo 10

python code

class stock_picking_barcode(models.Model): 
 _name = 'stock.picking'
 _inherit = ['stock.picking', 'barcodes.barcode_events_mixin']

 preset_quantity = fields.Float(store=False,
     default=1.0,
     help="The quantity will be applied to the pending product.")

 def on_barcode_scanned(self, barcode):
...

xml

<record model="ir.ui.view" id="view_picking_barcode_form">
     <field name="name">stock.picking.barcode.form</field>
     <field name="model">stock.picking</field>
     <field name="inherit_id" ref="stock.view_picking_form"/>
     <field name="arch" type="xml">
         <notebook position="before">
         <group attrs="{'invisible': [('state', 'not in', ('assigned'))]}" class="oe_edit_only">
             <field name="_barcode_scanned" widget="barcode_handler"/>
             <field name="preset_quantity" readonly="1"/>
         </group>
         </notebook>
    </field>
</record>

 

Avatar
Discard
Author Best Answer

I just found the cause myself. In the initial state, the default value is correct but the field is hidden as stated in "attrs =". When state is change to "assigned", the value is somehow cleanup, so that it seems the default value won't work.

Use "compute" instead of default to solve this 

preset_quantity = fields.Float(store=False, compute="_get_preset_quantity")
  @api.one
@api.depends('state')
def _get_preset_quantity(self):
  if self.state == 'assigned':
    self.preset_quantity = 1.0

Avatar
Discard
Related Posts Replies Views Activity
1
Jul 19
5400
4
Aug 17
6304
1
Dec 16
9761
7
Jul 16
5110
0
Feb 23
1786