Default values for a function field One2Many

289 views
Skip to first unread message

Jesús Martín Jiménez

unread,
Mar 12, 2014, 6:53:56 AM3/12/14
to tryto...@googlegroups.com
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,

Sergi Almacellas Abellana

unread,
Mar 12, 2014, 7:17:41 AM3/12/14
to tryto...@googlegroups.com
El 12/03/14 11:53, Jesús Martín Jiménez ha escrit:
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

Cédric Krier

unread,
Mar 12, 2014, 8:05:51 AM3/12/14
to tryto...@googlegroups.com
default method should be called even for Function field.
But at which moment are you expecting the call?

--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Jesús Martín Jiménez

unread,
Mar 12, 2014, 8:31:46 AM3/12/14
to tryto...@googlegroups.com
2014-03-12 13:05 GMT+01:00 Cédric Krier <cedric...@b2ck.com>:
On 12 Mar 03:53, Jesús Martín Jiménez wrote:
> 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?

default method should be called even for Function field.
But at which moment are you expecting the call?


When I make click over the new record icon of the one2many field.
 
--
Cédric Krier - B2CK SPRL
Email/Jabber: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/



--

Jesús Martín

Zikzakmedia SL
Dr. Fleming, 28, baixos
08720 Vilafranca del Penedès
☏ 93 890 21 08

Cédric Krier

unread,
Mar 12, 2014, 9:19:11 AM3/12/14
to tryto...@googlegroups.com
On 12 Mar 13:31, Jesús Martín Jiménez wrote:
> 2014-03-12 13:05 GMT+01:00 Cédric Krier <cedric...@b2ck.com>:
>
> > On 12 Mar 03:53, Jesús Martín Jiménez wrote:
> > > 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?
> >
> > default method should be called even for Function field.
> > But at which moment are you expecting the call?
> >
> >
> When I make click over the new record icon of the one2many field.

Creating a new record in a one2many will not trigger the default value
of the one2many but the default values of the target Model.

Jesús Martín Jiménez

unread,
Mar 12, 2014, 9:38:19 AM3/12/14
to tryto...@googlegroups.com
2014-03-12 14:19 GMT+01:00 Cédric Krier <cedric...@b2ck.com>:
On 12 Mar 13:31, Jesús Martín Jiménez wrote:
> 2014-03-12 13:05 GMT+01:00 Cédric Krier <cedric...@b2ck.com>:
>
> > On 12 Mar 03:53, Jesús Martín Jiménez wrote:
> > > 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?
> >
> > default method should be called even for Function field.
> > But at which moment are you expecting the call?
> >
> >
> When I make click over the new record icon of the one2many field.

Creating a new record in a one2many will not trigger the default value
of the one2many but the default values of the target Model.


Ok, thanks. I'll take it in account.
 
--
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