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

Get method to boolean allways returning same result?

By
Mihai Marius
on 5/7/14, 3:57 AM 852 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

ur question is not clear explain it with ur scenario

senthil
on 5/7/14, 3:59 AM

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.

Mihai Marius
on 5/7/14, 7:35 AM

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.

Mihai Marius
on 5/7/14, 8:51 AM
1

Nehal

--Nehal--
2090
| 5 2 8
Ahmadabad, India
--Nehal--
Nehal
On 5/7/14, 4:34 AM

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}

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!

Mihai Marius
on 5/7/14, 5:10 AM

It would be helpful if you post code.

Nehal
on 5/7/14, 5:32 AM

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

Nehal
on 5/7/14, 5:59 AM

Not working

Mihai Marius
on 5/7/14, 6:08 AM

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

Nehal
on 5/7/14, 6:36 AM

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.

Mihai Marius
on 5/7/14, 6:56 AM

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.

Mihai Marius
on 5/7/14, 7:03 AM

Posted a comment above.

Mihai Marius
on 5/7/14, 7:39 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: 5/7/14, 3:57 AM
Seen: 852 times
Last updated: 3/16/15, 8:10 AM