This question has been flagged
2 Replies
9567 Views

Hi,

I am trying to have a fields.function that have a default value for res_partner if it is a contact of a company, or if it is a company. But this value can be changed.

So I try this:

    def _passiv(self, cr, uid, ids, field_names, arg,  context):
            res = {}
            for partner in self.browse(cr, uid, ids, context):
                res[partner.id] = False
                if partner.parent_id:
                    res[partner.id] = True
                if partner.is_company:
                    res[partner.id] = True
            return res

        def _set_passiv(self, cr, uid, ids, name, value, arg, context=None):
            if not value: return False
            if type(ids)!=type([]):
                ids=[ids]

            for partner in self.browse(cr, uid, ids, context=context):
                cr.execute("""update res_partner set
                        passiv=%s where id=%s""", (value, partner.id))
            return True


_columns = {
    'passiv': fields.function(_passiv, fnct_inv = _set_passiv, store = True, string='Passiv', type='boolean', help="Check if it is a passiv"),
}

This code give me right values to default, but if I edit my field and save it this doesn't save my choice. I can't access to vals or any other variable that have the value that the user gave to the field.

So, I have it not readonly but I can never change the value.

Any ideas?

Avatar
Discard

Hello Anabela, Have you found any solution? If yes, can you please post it as i am facing same problem in case of function field. Regards,

Best Answer

I was struggling with the same problem. readonly=False on the model side didn't seem to affect anything.

In my case the solution was to add readonly="0" on the view side. Here's the field tag:

<field name="note_template" class="oe_edit_only" readonly="0" nolabel="1" />
Avatar
Discard
Best Answer

Maybe if you change you field to boolean and on defaults, get the partner is a company or not...

def _get_company(self,cr,uid,context=None):
     result="(get if is a company and return True or False)"
     return result

     _columns = {
        'passiv': fields.boolean('Passiv', help="Check if it is a passiv"),
    }

    _defaults = {
       'passiv': _get_company,
}
Avatar
Discard