El 12/03/14 11:53, Jesús Martín Jiménez ha escrit:
Hi,

I would like to add a One2Many field in a model without modifying the target model, so I'm trying to make this field as a funtional one. Now, I want to set the default values on each new record created through the setter method of this field, but seems that de default_field() method on a function field is not called. Is it correct? I'm missing something?

Thanks,

You can use the context[1] to pass a flag to check if you're creating the target model from your One2Many field and override the default function to get the value depending on the context.

It will be something like this:

lines = fields.One2Many(xxx, xxx, xxx,
    context={'from_my_model': True})

and in the target model you will do:

@classmethod
def default_field(cls):
    if 'from_my_model' in Transaction().context:
           return your_value
    return super(Class, cls).default_field()


[1] http://doc.tryton.org/3.0/trytond/doc/ref/models/fields.html#context

--
Sergi Almacellas Abellana
www.koolpi.com
Twitter: @pokoli_srk

Reply via email to