This question has been flagged
3 Replies
10175 Views

I have a editable tree view with 3 fields of a model. I would like to add a button to show a form to edit the rests of the fields of the model. How can I do it?

Avatar
Discard
Best Answer

You can add a button on tree and pass action of that form in name. like :


<button name="%(action_id_of_form_you_want_to_open)d"  type="action" string="Open Form"/>
 

Avatar
Discard
Author

Hi. Thanks for your answer but this just shows a form with no empty values in fields and no save (nor cancel) button. I need the form to edit the current record. Do I need to set some value in context dictionary.

Author Best Answer

Hi.

Thanks for your answer but this just shows a form with no empty values in fields and no save (nor cancel) button. I need the form to edit the current record.

Do I need to set some value in context dictionary.

Avatar
Discard
Best Answer

You can add a button, as Yogesh said, but you'll want to make it type object. This will link to a function in the tree's model class, and this function can return a new view. For example, in the xml:

<button name="button_details" string="Details" type="object"/>

Then in the class definition for this model:

    @api.multi
    def button_details(self):
        view = {
            'name': _('Details'),
            'view_type': 'form',
            'view_mode': 'form',
            'res_model': 'my.custom.model',
            'view_id': False,
            'type': 'ir.actions.act_window',
            'target': 'new',
            'readonly': True,
            'res_id': self.id,
        }
        return view

Make sure to replace 'my.custom.model' with the actual model you want to edit. Also, you'll need to have already definied the ir.actions.act_window and ir.ui.view records for that model, or Odoo won't know how to bring up the details. That top line there is from the new v8 api, but if you're using v7, remove it and just add the standard args to the button_details function call (cr, uid, ids, context). There are lots of examples of button functions in the code, I'm sure you can find one. The key to the button bringing up a new modal window is the line :

'target': 'new',

This will make the details edit be its own little popup window. Alternativly, you can set target to 'current' to just redirect to that page normally, but I'm going to guess in this instance you'd rather a little popup and then return to the main view.

 

EDIT: Oops, I forgot that the "'res_id': self.id," line is very important, and I think it only works with the v8 API. If you're using v7, instead of "self.id", you'll probably want the "ids" arg passed in from the function arguments. I hope that makes sense.

Avatar
Discard
Author

Hi, Ben. Thanks a lot. I haven´t test yet but that looks like the solution.

Author

Doesn´t this method should return something like {'domain': {'field':'domain'}, 'value' : 'field2': 'text'}?

Author

Well, I guess it works in v8, but in OpenERP 7 parameter 'res_id' must be something else.