Odoo Help


Little help with 'action' required.

Dr Obx
on 9/2/15, 6:02 PM 813 views

Hi everyone.

Need help with 'action'.

What I would like to achieve is on click 'BUTTON' change state in each record in table depending on value in specified field.

For example: if value in <field name="pnp"/> = 'rom' change state in <field name='state'/> to arm else change state to wrh.

Can you help me with ?




| 5 3 8
Delhi, India

Nothing to say .... :)

On 9/2/15, 9:09 PM

From what I understood you want a method on a button which changes state based on some value

def change_state(self):
    #serach all records
    records = self.env['your.object.name'].search([('state','=','new_order')])#this will search all records in the given object with state == 'new_order
    #you have record set of all records in state 'new_order' now you can iterate over using for loop
    for record in records:
        #now record is single recordset you can access any field of your object with dot ('.') operator
        if record.pnp == 'rom': # no need to check state more becuase now you have only filtered records with state 'new_order' only
            record.write({'state' : 'royal'}) #'royal' state must be present in options
            record.write({'state' : 'in'}) #'in' state must be present in options    

Great thanx Yogesh, But in this form it's doing only one action (for one record) and all it's doing is .... a new empty record instead of changing state. Also what if I would like to select records by more than one field ? For example and ? and if i would like to check a value with 'like' or 'ilike' ?

Dr Obx
on 9/3/15, 4:15 AM

Let Me explain what do I need to achieve: I have a list of records with 'state' = 'new order', second field which is important in this process is 'pp' which can held either 'Royal...' or 'Other' values, based on this two fields i would like to change 'state' accordingly to the value held by 'pp'. In one click whole table where 'state' = 'new order' and 'pp' = 'Royal..' should get a new 'state' = 'royal' and records with 'state' = 'new record' and 'pp' = 'Other...' should get a new 'state' = 'in' ..... something like that.

Dr Obx
on 9/3/15, 4:23 AM
in loop

for each record where 'state' = 'new_order'

if 'state' == 'new_order' && 'pp' = 'Royal....'
    change_state to 'royal'

if 'state' == 'new_order' && 'pp' = 'Other....'
   change_state to 'in'
Something like that :) If would you help me with ...
Dr Obx
on 9/3/15, 4:29 AM

And how to apply loop which will change all records accordingly to the if statements ?

Dr Obx
on 9/3/15, 7:29 AM

please check again, I have updated answer based on your last comments.

on 9/3/15, 9:01 AM

I'm pretty sure that everything is placed correctly but it gives me an error:

    return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
AttributeError: 'mymodel.mane' object has no attribute 'change_state'
Dr Obx
on 9/3/15, 9:39 AM

@Rob: Please check if you are defining the method under "mymodel.mane" class only or not.

on 9/4/15, 1:52 AM

Hi Pawan, Yes I checked it, everything looks ok, now it doesn't give me any errors but still "doing nothing" :(

Dr Obx
on 9/4/15, 3:02 AM


#----------------- Main class -----------------    
class iprodstep_log(models.Model):
    _name = 'iprodstep.log'
    _description = 'iprodstep log'
    _inherit = 'ir.needaction_mixin'

    def sort_order(self):
        records = self.env['iprodstep.log'].search([('state','in',['draft'])])
        for record in records:
            if record.paid_on_date == '':
                record.write({'state' : 'unpaid'})
        if record.postage_service == 'Royal Mail 1st Class':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail 2nd Class':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'International Tracked Postage':
            record.write({'state' : 'royal','urgent_id': True})
        elif record.postage_service == 'Royal Mail 1st Class Signed For':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail 2nd Class Signed For':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail International Signed':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail International Tracked':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Royal Mail International Standard (Small Packets)':
            record.write({'state' : 'royal'})
        elif record.postage_service == 'Other Courier 3-5 days':
            record.write({'state': 'in'})
        elif record.postage_service == 'Other 24 Hour Courier':
            record.write({'state': 'in','urgent_id': True})
            record.write({'state' : 'in'})

Dr Obx
on 9/4/15, 3:09 AM

Ok, I managed to do it, it works now but adding one empty record to the list. Weird :)

Dr Obx
on 9/4/15, 5:50 AM

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

1 follower(s)


Asked: 9/2/15, 6:02 PM
Seen: 813 times
Last updated: 9/3/15, 9:00 AM