Contributors mailing list archives

contributors@odoo-community.org

Write record in onchange so it behaves like a button?

by
Open for Small Business, Graeme Gellatly
- 16/02/2018 03:48:15
As Odoo freely admits its js barcode integration is pretty broken via button triggers and unfixable without an entire rewrite.   Just little things like applying the trigger to the entire breadcrumb list or the first item or no items.

But using on_barcode_scanned works mostly as intended.  The main issue in simulating a button press this way is that onchange works on a newid object, whereas buttons don't, so objects with lots of computed fields go haywire. 

What is the best way to write the record and then pass it on to the original button function? 

class MrpWorkorder(models.Model):

_inherit = ['mrp.workorder', 'barcodes.barcode_events_mixin']
_name = 'mrp.workorder'
BARCODE_PREFIX = 'WO.'

def on_barcode_scanned(self, barcode):
if barcode.startswith(self.BARCODE_PREFIX):
try:
return getattr(
barcode[len(self.BARCODE_PREFIX):], self)
except AttributeError:
return {'warning': {
'title': 'Error',
'message': '%s is not implemented' % barcode}}
return