Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

A default value isn't applied on client view! Odoo 10

By
Monchai P
on 10/13/16, 2:47 AM 312 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>

 

0
Monchai P
On 10/13/16, 11:51 PM

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

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 10/13/16, 2:47 AM
Seen: 312 times
Last updated: 10/13/16, 11:51 PM