Skip to Content
Menu
This question has been flagged
4 Replies
8359 Views

I've inherited the pos.session model with a function triggered when an index changed:

class divina_session(models.Model):        
    _inherit = 'pos.session'
  order_paid = fields.Integer('Order Paid', store=True, default='0')

@api.multi
def _write(self, vals):
res = super(divina_session, self)._write(vals)

for record in self:
record.set_data_write(vals, record)
return res
    @api.multi    
    def set_data_write(self, vals, record):        
        if vals.get('order_paid'):
            my code here

The method is used to update a whole record on another model (pos.adm).

Pos.adm has a tree view, and I would like to update the view from button, since it doesn't update automatically after db changed... However I don't know what syntax should I use on my button definition and, most important, how to trigger it on pos.session since my index (order_paid) will not change on button press. 

Any clue?

Thanks.

F.

Avatar
Discard
Author

Solved (for now) calling directly the menu item from a button in my view. Simple and effective, even with a little drawback. 

<button name="%(adm_sessions_tree_view2)d" string="Update" type="action"
class="oe_highlight" attrs = "{'invisible':[('state','!=','opened')]}"/>

This will  update the view exactly like will do when calling the menu item itself (that is hidden on a 2 levels submenu), without the need to refresh the whole page. I'm still working on it to force an update manually but for now I can spend my effort on other issues I need to solve with other functions.

Author Best Answer

Solved, even if  with a not so elegant, pythonic way.

On pos.adm I created a new def since is quite easy search for the correct one (pos_session.name =is equal to pos_adm.pos_ref):

@api.multi
def _update_tree(self):

pse = self.env['pos.session'].search([('name', '=', self.pos_ref)])
pse.write({'order_paid': pse.order_paid + 1})

Since order_paid field is just an index used to trigger function is not a problem use it to force a db check.

After that I simply wrote some lines of xml for a function called by a button.

    <record id = "update" model = "ir.actions.server">
<field name = "sequence" eval = "5"/>
<field name = "name">update_session</field>
<field name = "model_id" ref = "model_pos_adm"/>
<field name = "condition">True</field>
<field name = "type">ir.actions.server</field>
<field name = "state">code</field>
<field name = "code">
pos_obj = env['pos.adm'].browse(context.get('active_id'))
pos_obj._update_tree()
</field>
</record>

The button call:

<button name="%(update)d" string="Up" type="action"
class="oe_highlight" attrs = "{'invisible':[('state','!=','opened')]}"/>

Now I have a quicker real time update on my tree view.   

Avatar
Discard
Best Answer

Hi,

Inside the write function you can achieve this, check whether Vals contains order_paid value, if so you can execute your action. First of all what is the relation between pos.session and pos.admin ? You have to get it, then if you need to execute a function in the pos.admin model, you can search the records of pos.admin model and iterate over the loop and call the function.

@api.multi
def _write(self, vals):
res = super(divina_session, self)._write(vals)
if vals.get('order_paid'):
# if there is any search filters you can give it
pos_admin_rec = self.env['pos.admin'].search([])
for pos_rec in POs_admin_rec:
#here call the function in pos.admin model
pos_rec.function_name()
return res


Thanks

Avatar
Discard
Author

Niyas, thanks for your reply.

When an order is marked as paid, vals is reached two times (don't know exactly why) with tho different values. THe first time gets 'sequence_number' and the second one gets 'order_paid':

vals {'sequence_number': 17}

vals {'order_paid': 2}

Then I can intercept vals and make my call.

There is not a direct relation (nor related field) between pos.session and pos.adm.

The function on pos.session will create the basic data on pos.adm, and then the record will be populated from another function on pos.adm.

I know how to reach a function on another model (thanks anyway!) but the problem is calling by a button. I have update the thread title because was not clear.

Related Posts Replies Views Activity
0
Dec 24
19
2
Sep 22
5954
7
Sep 21
25968
3
Dec 23
18296
2
May 17
6220