跳至内容
菜单
此问题已终结
1 回复
6438 查看

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>

 

形象
丢弃
编写者 最佳答案

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

形象
丢弃
相关帖文 回复 查看 活动
1
7月 19
6502
4
8月 17
7444
1
12月 16
10601
7
7月 16
6149
0
2月 23
3075