Odoo Help

Welcome!

This community 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.

0

[solved] domain restriction and clear the previously selected value of a many2one field

By
Romuald Franck
on 10/22/13, 9:15 AM 2,671 views

Hi to everyone!

I have two many2one fields (list1 and list2) in the same class and view. I want when a user select a value from list1, the list2 to automatically update with a restriction domain value. this is done

But I also want the 'previously' selected value of list2 to be drop so that the user is force to reselect it, in the case he has previously selected a value in list2 and then reselect in list1. Because domain restriction only update the list2 but leave the previously (now not pertinant) value of list2 and the user can pass. Thx to any help!

1

Brett Lehrer

--Brett Lehrer--
1252
| 5 4 8
Milwaukee, United States
--Brett Lehrer--
Director of IT and open source enthusiast.
Brett Lehrer
On 10/22/13, 1:23 PM

You need to add an onchange event to list1. Would look something like this in the XML:

<field name="list1" on_change="my_onchange_function(list1)"/>

Then you'd need a function in that object with the name listed in on_change, which is hopefully a better name than my example:

def my_onchange_function(self, cr, uid, ids, list1, context=None):
    if context is None: context = {}
    # If there's a value in list1 now, empty list2.
    res = {}
    if list1:
        res['list2'] = False
        res['list3'] = False
        res['some_number'] = 0
    return {'value': res}

You could add in a few more features to make the onchange function a bit smarter, but that will basically do the job.

Thank you it works. I have put another on_change action on list2 and it was that which gave me errors. I have fixed it. Please How to return many value for several fields at a time. Thx

Romuald Franck
on 10/24/13, 6:18 AM

Just keep adding dictionary entries to res. I'll edit the answer to change multiple values at once.

Brett Lehrer
on 10/24/13, 10:37 AM
0
Romuald Franck
On 10/24/13, 4:19 AM

I tried it but got and error. Please look . Here is my onchange def :

def on_change_salle_de_classe_id(self,cr,uid,ids,salle_de_classe_id,context=None):
    salle_de_classe = self.pool.get('schoolem.salle_de_classe').browse(cr,uid,salle_de_classe_id)

    cours_obj = self.pool.get('schoolem.cours')
    ids1 = cours_obj.search(cr, uid, [('niveau_id.id', '=', salle_de_classe.classe_id.niveau_id.id)])
    ids2 = cours_obj.search(cr, uid, [('niveau_id.id', '=', salle_de_classe.classe_id.niveau_id.id),('specialite_id.id', '=', salle_de_classe.classe_id.specialite_id.id)])
    if context is None: context = {}
    # If there's a value in list1 now, empty list2.
    res = {}        
    if salle_de_classe_id:
        res['cours_id'] = False
    return {'value': res}
    #return {'domain':{'cours_id':[('id','in',ids1+ids2)]}}
    #return {'value':{'cours_id':False},'domain':{'cours_id':[('id','in',ids1+ids2)]}}

but I got this error :

ProgrammingError: ERREUR: l'opérateur n'existe pas : integer = boolean LINE 1: ....id FROM "schoolem_cours" WHERE schoolem_cours.id IN (false)...
^ HINT: Aucun opérateur ne correspond au nom donné et aux types d'arguments. Vous devez ajouter des conversions explicites de type.

This is a type casting error due to the fact that one of the IDs you're filtering on is actually not set at all. Make sure you're checking that ids1 and ids2 both have values in them first before returning them into the domain. Both are currently returning empty lists, which translates to False, generating an error down the road. Also, since ids2 is a subset of ids1, there will be duplicate ids in the combined list, and a quick way to fix that before passing it along is to use list(set(ids1+ids2)). Convert to a set to remove duplicates, then back to a list.

Brett Lehrer
on 10/24/13, 4:44 AM

I tried this :return {'domain':{'cours_id':[('id','in',list(set(ids1+ids2)))]},'value':{'cours_id':False}} but still have the same error. Even without using domain

Romuald Franck
on 10/24/13, 4:55 AM

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 10/22/13, 9:15 AM
Seen: 2671 times
Last updated: 3/16/15, 8:10 AM