Skip to Content
Menu
This question has been flagged
1 Reply
3694 Views

Hello

I am trying to use the @api.onchange with two dropdown menus and although I am not getting any errors, it is not working how I would like.

I have 2 fields created:


"status_id": fields.many2one("profile.status", "Kontaktstatus"),

"phase_id": fields.many2one("profile.phase", "Phase"),


and I would like that when I change the phase_id that the status_id will change automatically based on what is selected. Here is my def code:


@api.onchange('phase_id')

def do_stuff(self):

if self.phase_id != '':

    if self.phase_id == "Kontaktphase":

        self.status_id == "Interessant"

    elif self.phase_id == "Präsentationphase":

        self.status_id == "Kandidat"

    elif self.phase_id == "Testphase":

        self.status_id == "Kandidat"

    elif self.phase_id == "Angebotsphase":

        self.status_id == "Kandidat"


What am I doing wrong here?

Avatar
Discard
Best Answer

Hi!,

You are using many2one field here ("status_id","phase_id").So You cant compare id (many2one field) with string .

Try to comapre with many2one field name with string value.

Ex :-

if self.phase_id.name == "Kontaktphase":

and return your value using below code

result['value']['status_id'] = ID value of "Interessant"

Avatar
Discard
Author

Thanks Saravanan Tried the above and I got the following error: Traceback (most recent call last): File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 530, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 567, in dispatch result = self._call_function(**self.params) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 303, in _call_function return checked_call(self.db, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/service/model.py", line 113, in wrapper return f(dbname, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 300, in checked_call return self.endpoint(*a, **kw) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 796, in __call__ return self.method(*args, **kw) File "/usr/lib/python2.7/dist-packages/openerp/http.py", line 396, in response_wrap response = f(*args, **kw) File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 936, in call_kw return self._call_kw(model, method, args, kwargs) File "/usr/lib/python2.7/dist-packages/openerp/addons/web/controllers/main.py", line 928, in _call_kw return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 241, in wrapper return old_api(self, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/api.py", line 363, in old_api result = method(recs, *args, **kwargs) File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5846, in onchange record._onchange_eval(name, field_onchange[name], result) File "/usr/lib/python2.7/dist-packages/openerp/models.py", line 5727, in _onchange_eval method_res = method(self) File "/home/vagrant/addons/profile/profile.py", line 78, in do_stuff result['value']['status_id'] = "Interessant" NameError: global name 'result' is not defined

define variable in the top of function result = {'value': { }}

Plz check above answer ,I have edited.

Author

Thank Saravanan, I must been doing something very wrong as it is not liking the sytax or the "ID value of" in the above code. @api.onchange('phase_id') result = {'value': { }} def do_stuff(self): if self.phase_id != '': if self.phase_id.name == "Kontaktphase": result['value']['status_id'] = ID value of "Interessant" When I put result inside the function, it says it should be a global variable.

Hi,then Just define global variable as result = {'value': {'status_id':False}}

Author

I am now back to original error: Syntax error on this line: result['value']['status_id'] = ID value of "Interessant"

Author

Any other ideas on this as this is still not working

Related Posts Replies Views Activity
1
Oct 23
361
2
Oct 23
668
2
Aug 23
1958
4
Aug 23
18245
3
Oct 22
8900