Search code examples
odooodoo-view

Odoo 10 - Form view opened in edit mode


I'm developing on Odoo 10.

I created a dynamic form view which search and display a product from its barcode, but I've got a problem.

Since the view has no initial record to display it is opened in edit mode, and that's ok, because I want to type the 'barcode' field.
But, after the product is displayed, when I exit from that view the 'can_be_discarded' function is fired, opening the confirm dialog.

Have I to create a new view type inheriting from FormView or is there a way to workaround this problem?


The view is a classic form view, with nothing special.
Here's the server code instead.

class ProductFromBarcode(models.TransientModel):
    _name = 'levelprime_product_general_status.product_from_barcode'
    _inherits = { 'product.product': 'product_id' }

    product_id      = fields.Many2one(
                            comodel_name='product.product',
                            store=False)
    product_barcode = fields.Integer(help='Insert the barcode to search '
                                          'the correspondent product',
                                     store=False)

    @api.onchange('product_barcode')
    def on_barcode_changed(self):
        if self.product_barcode != 0:
            self.product_id = self.get_product_from_barcode(self.product_barcode)

    @api.model
    def get_product_from_barcode(self, barcode):
        r = self.env['product.product'].search([('barcode', '=', barcode)])
        if r:
            return r

Solution

  • Ok, I think I'm in the right way, there are some rendering problem to solve, but the main behavior is what I was searching for.

    I created a new type of view which inherit from the 'FormView' one and override the 'can_be_discarded' method to not execute controls about the data changes.


    JS

    odoo.define('levelprime_product_general_status.readonly_formview', function(require) {
        'use strict'
    
        var core = require('web.core')
        var FormView = require('web.FormView')
    
        var ReadOnly_FormView = FormView.extend({
            init: function() {
                this._super.apply(this, arguments)
            },
            start: function() {
                this._super.apply(this, arguments)
            },
            can_be_discarded: function() {
                return $.Deferred().resolve()
            }
        })
    
        core.view_registry.add('readonly_form', ReadOnly_FormView)
    
        return ReadOnly_FormView
    })
    

    PY

    class ViewExtension(models.Model):
        _inherit = 'ir.ui.view'
    
        type = fields.Selection(selection_add=[
            ('readonly_form', 'ReadOnly Form Version')])
    

    Then you can simply use the tag in the xml.