Modify a dict field key/value using a function field

44 views
Skip to first unread message

Richard V.

unread,
Jun 18, 2016, 5:50:04 AM6/18/16
to tryton
Hello Tryton Community,

I was trying to modify a dict field key's value directly with a function field through it's setter and getter class functions, but the value doesn't get stored into the DB after the record is saved. I want to store some value into dat[num1], directly using the function field, but it does not work. Does anyone know why? I have attached part of the code below for inspection. I have created a module that extends the Company class for testing purpose. Can anyone guide me on this?

class Company:
    __metaclass__
= PoolMeta
    __name__
= 'company.company'
    dat
= fields.Dict('company.companyvardict','dat')
    num1
= fields.Function(fields.Integer('Num1', depends=['dat'],
        states
=STATES),'get_dats', setter='set_dats')
   
   
@classmethod
   
def get_dats(cls, companies, names):
        result
= {k:{} for k in names}
       
for c in companies:
           
for n in names:
               
if not c.dat:
                    result
[n][c.id] = None
               
else:
                    result
[n][c.id] = c.dat[n]
       
return result

   
@classmethod
   
def set_dats(cls, companies, name, value):
       
for c in companies:
           
if not type(c.dat) is dict:
                c
.dat = {}
           
if value:
                c
.dat[name] = value
           
elif name in c.dat:
               
del c.dat[name]



Many thanks in advanced for the help.

   

Cédric Krier

unread,
Jun 18, 2016, 6:25:04 AM6/18/16
to tryton
On 2016-06-17 13:57, Richard V. wrote:
> @classmethod
> def set_dats(cls, companies, name, value):
> for c in companies:
> if not type(c.dat) is dict:
> c.dat = {}
> if value:
> c.dat[name] = value
> elif name in c.dat:
> del c.dat[name]

Modification of the instances in the setter method are not saved by
default. So you have to call yourself the write method or save the
instances. I'm not sure if saving will not create a loop, I never did
such thing so it must be tested.
I have created issue5651 to clarify the behaviour:
https://bugs.tryton.org/issue5651

Also if you go with the save, changing in-place the dict will not be
detected as a change. You have to re-assign the dictionary. See
https://bugs.tryton.org/issue5652

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/
Reply all
Reply to author
Forward
0 new messages