Skip to Content
Menu
This question has been flagged
2 Replies
11752 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!

Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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

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

Author Best Answer

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.

Avatar
Discard

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.

Author

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

Related Posts Replies Views Activity
1
Oct 22
2985
8
Feb 17
7448
0
Jan 17
5170
4
May 24
16578
1
Oct 22
2502