This question has been flagged
1 Reply
4149 Views

Hi guys,

I have a get method in a class to a boolean field: Method:

    def get_method(self, cr, uid, ids, field1, context=None):
    res = {}
    if field1==True:
        res = '1'
    else:
        res = '2'
    return res

The field definition: 'field1': fields.boolean('Field1')

The problem is that this allways returns '2' no matter if field1 is True or False. I'm using the method in an onchange method to another class to update a selection field, which is having possible values 1 and 2.

Also please consider the fact that i'm very beginner...

Many thanks in advance!

More details:

I have class A and class B under same custom module. Under class A i have an onchange method in a field many2one. The many2one is also present in class B (like partner_id present in res_partner_address and crm_lead under CRM module :)), so this way i'm retrieving some fields value using this onchange method under class A. The onchange under class A is also using the method described above, to get the values of class B boolean field1 and transform its True or False value to 1 or 2 and returning it to the onchange method. Practically i'm trying to change boolean field from class A to a selection field in class B. Field selection in class A has the following definition:

'contorizat': fields.selection([('1', 'C'),('2', 'N')], 'Contorizat'),

The onchange method and the get method works fine with only one issue: the get method is allways returning 1 no matter the value of the boolean field1 under class B is True or False. So i'm not sure if this can be done on boolean field or there is something i'm doing very wrong. I hope this is explained ok otherwise i will need to get all the code here.

Many thanks for the fast reply !

Update codes: Class A: field many2one:

'apartament_id': fields.many2one('bloc.apartamente', 'Numar apartament'),

field selection which is getting updated by the onchange method:

'contorizat': fields.selection([('1', 'C'),('2', 'N')], 'Contorizat fake'),

onchange method in class A:

    def onchange_apt_id(self, cr, uid, ids, add, nr_ctr_pers, nr_persoane, su, plateste_fr, contorizat,  context=None):
    data = {'value': {'nr_ctr_pers': False}}
    if add:
        apt = self.pool.get('bloc.apartamente').browse(cr, uid, add)
        data['value'] = {'nr_ctr_pers': apt.locatari_prezenti or False,
                         'nr_persoane': apt.numar_locatari or False,
                         'su': apt.suprafata_utila or False,
                         'plateste_fr': apt.fond_rulment or False,
                         'contorizat': apt.get_contorizat(cr, uid, ids)}
        return data

Class B: field boolean:

'apa_calda_contorizat': fields.boolean('Contorizat apa calda'),

get method:

    def get_contorizat(self, cr, uid, ids, apa_calda_contorizat,  context=None):
    res = {}
    a = apa_calda_contorizat
    if a==True:
        res = '1'
    else:
        res = '2'
    return res
Avatar
Discard

ur question is not clear explain it with ur scenario

Author

Class A and Class B have in comun field many2one 'apartament_id'. In Class a the onchange method onchange_apt_id is applied on this field. The field value updated in Class A (which is the issue here) is field selection 'contorizat' - last on ein the onchange method which is calling the get_contorizat method.

Author

I changed the boolean field on Class B to be a selection field so now with the necesary changes, the onchange method works without the get_method. But still don't understand why the get method does not work on that boolean field on class B. So for the sake of knowledge, if anyone knows why please update.

Best Answer

Hi,

Try the following code: In your xml file:

<field name="field1" on_change="onchange_field1(field1)"/>

In py file:

def onchange_field1(self, cr, uid, ids, field1=False, context=None):
    if field1:
        val = { 'field2': True}
    else:
        val = { 'field2': False}
    return {'value': val}
Avatar
Discard
Author

Hi Nehal, i updated the question. This will not work as there are 2 different classes. Please review question. If still not clear i will post classes and methods code... Many thanks and sorry for the misunderstanding!

It would be helpful if you post code.

Try to return value like res = { 'contorizat': '1'}

Author

Not working

how class B related to classA in your module? if you have value True in your field1, which record should be updated with result ?

Author

Class A and Class B have in comun field many2one 'apartament_id'. In Class a the onchange method onchange_apt_id is applied on this field. The field value updated in Class A (which is the issue here) is field selection 'contorizat' - last on ein the onchange method which is calling the get_contorizat method.

Author

Class A and Class B have in comun field many2one 'apartament_id'. In Class a the onchange method onchange_apt_id is applied on this field. The field value updated in Class A (which is the issue here) is field selection 'contorizat' - last on ein the onchange method which is calling the get_contorizat method.

Author

Posted a comment above.