1

I wanted to know that is this possible in OpenERP-7 to call a form through a button in read-only mode. Like the form is editable through some buttons but I want to put some restrictions through a specific button . I want that when a user clicks the button the form opens in read-only mode and user cannot create/edit/delete any of the records. I tried to use this in my .py file:

 def views(self,cr,uid,ids,context):
     for id in ids:
         deg_obj=self.pool.get('deg.form').browse(cr,uid,id)
         my_id=int(deg_obj.my_products)
     ss= int(deg_obj.categ_temp2)   
     domain = [('categ_id','=',ss)]
     return {
         'type': 'ir.actions.act_window',
         'name': _('Product'),
         'res_model': 'product.product',
         'view_type': 'form',
         'view_mode': 'tree,form',
         'target': 'current',
         'domain': domain,
         'store':False,
         'create':False,
         'edit':False     
               }

and this is my xml code:

             <button name="views" string="VIEW PRODUCT" type="object" /> 

But its not working with this. I dont know if I have to put any attributes in the xml code or what. Because The form will be in view mode only with this button call. Any help or guidance will be appreciated. Thanks all

Arsalan Sherwani
  • 889
  • 2
  • 25
  • 60

2 Answers2

0

If you would like input objects to open in read-only mode, then it you would have to add this attribute on the read-only input field, as such:

 <input type="text" readonly/>

Same thing goes if you want it disabled.

 <input type="text" disabled/>

Using jQuery, it's simply done like this to apply:

 $("input").attr("readonly",true);
 $("input").attr("disabled",true);

And like this to remove:

 $("input").removeAttr("readonly");
 $("input").removeAttr("disabled");

The different of using the 2 is that when using disabled the value will not get submitted in the form post, whereas using readonly will.

This works for most input type's, except for radio/checkbox inputs. For these types of input fields, you can view this helpful post regarding the topic: Can HTML checkboxes be set to readonly?

A simple function to do this on your page would be like this:

      function makeReadOnly() {
           $("form :input").attr("readonly",true);
      }

      function makeEditable() {
           $("form :input").removeAttr("readonly");
      }
Community
  • 1
  • 1
Control Freak
  • 12,965
  • 30
  • 94
  • 145
  • First , I want the form to be opened in view mode. Not any specific field.In OpenERP, I dont have much idea of using jquery. – Arsalan Sherwani Aug 10 '14 at 11:39
  • You don't need jQuery. How do you load the form fields? However they are loaded, simple add a `readonly` attribute, and it will be readonly in the browser. – Control Freak Aug 10 '14 at 12:01
  • Look the form fields are loaded with the form. Like my model is product.product as you see above. When u load the form , the fields will be loaded. Now I have to either set the attributes within the function of the button or something else. – Arsalan Sherwani Aug 10 '14 at 12:12
  • Then use jquery. Add it to your page, and you can do this as a function so you can call it in the button click or on an onLoad event. You can see the function in my answer that will do this – Control Freak Aug 10 '14 at 12:14
  • I have no idea of using jquery with xml in OpenERP development. Are you sure this is achievable with jquery? . Can you guide me a little more deep – Arsalan Sherwani Aug 11 '14 at 05:58
  • Well you put `jquery` tag in your post. You can try looking here: http://stackoverflow.com/questions/1362153/how-can-i-use-jquery-to-make-an-input-readonly – Control Freak Aug 11 '14 at 06:07
0

You can pass a context from the button, and can set attrs like following in all the fields of form like below:

<field name="field_name" attrs="{'readonly':[('your_context','=',True)]}">
sjpatel
  • 184
  • 6