Default value related to the latest data entered ...

82 views
Skip to first unread message

Maxime Richez

unread,
May 23, 2016, 5:36:31 AM5/23/16
to tryton
Hello,

Is it possible to define a default value in a field that depends on the previously entered data?

For example, I have a model with detail rows (product, quantity, prix_unitaire, currency):

By default the value of my currency is the currency of my company.
If I have to introduce more detail lines with a currency different from that of my company, to avoid to select this currency every time, is it possible to imagine that the default value of the currency come from the value of the previous detail row (and if it's the first line the currency of the company)? did the context keeps the last entered values?

Thanks for your help...

Dominique Chabord

unread,
May 23, 2016, 6:38:08 AM5/23/16
to try...@googlegroups.com
hello,

the default values is a full topic by itself.
Collecting requirements and constraints is a good idea.
I don't think your proposal does the trick, since default values must
comply with basic security rules.
> --
> You received this message because you are subscribed to the Google Groups
> "tryton" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/tryton/17aec7dd-f411-463d-8bbc-f7a64ad5adb3%40googlegroups.com.



--
Dominique Chabord - SISalp
Logiciel libre pour l'entreprise Tryton et open-source Odoo, OpenERP
18 avenue Beauregard 74960 Cran Gevrier
145A rue Alexandre Borrely 83000 Toulon
tel(repondeur) +33(0)950274960 fax +33(0)955274960 mob +33(0)622616438
http://sisalp.fr
http://openerp-online.fr

Sergi Almacellas Abellana

unread,
May 23, 2016, 6:45:20 AM5/23/16
to try...@googlegroups.com, Maxime Richez
You can use an on_change_with that depends on the parent field which will be called by the tryton client and you can compute the value as you want.
>
>Thanks for your help...

Hope it helps!

--
Sent from my Android device with K-9 Mail. Please excuse my brevity.

Maxime Richez

unread,
May 23, 2016, 7:02:25 AM5/23/16
to tryton, maxime...@gmail.com

You can use an on_change_with that depends on the parent field which will be called by the tryton client and you can compute the value as you want.
>

i think there's no parent field, there's multiples lines...

Cédric Krier

unread,
May 23, 2016, 7:05:03 AM5/23/16
to tryton
On 2016-05-23 02:36, Maxime Richez wrote:
> Hello,
>
> Is it possible to define a default value in a field that depends on the
> previously entered data?
>
> For example, I have a model with detail rows (product, quantity,
> prix_unitaire, currency):
>
> By default the value of my currency is the currency of my company.
> If I have to introduce more detail lines with a currency different from
> that of my company, to avoid to select this currency every time, is it
> possible to imagine that the default value of the currency come from the
> value of the previous detail row (and if it's the first line the currency
> of the company)?

If it is stored in the database, then you can make a query to find the
last entered. But this works only if it is stored in the database so
not for One2Many list.

> did the context keeps the last entered values?

No, nothing is stored in the context. The context is a static things.

For the One2Many case, it is usually better to not store default value
on the line but one at the parent.

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

Maxime Richez

unread,
May 23, 2016, 7:32:02 AM5/23/16
to tryton


If it is stored in the database, then you can make a query to find the
last entered.  But this works only if it is stored in the database so
not for One2Many list.

   And in my case it's a One2Many :(



For the One2Many case, it is usually better to not store default value
on the line but one at the parent.


Effectively it's the best solution to store at the parent, but in our case whe need the value on the lines (because at the next step of the module, we are working on the lines ...)
 

Cédric Krier

unread,
May 23, 2016, 7:50:04 AM5/23/16
to tryton
I think in this case, it is better to define a default one on the parent
and use the on_change like @pokoli said to change the default value to
this default defined on the parent.

Maxime Richez

unread,
May 24, 2016, 11:01:00 AM5/24/16
to tryton


I think in this case, it is better to define a default one on the parent
and use the on_change like @pokoli said to change the default value to
this default defined on the parent.
 

I follow your advice but how to force the "on_change_with" be executed and set the default value when the form is opened ? Here's is the code of my on change:

@fields.depends('_parent_purchase_requisition.currency')
   
def on_change_with_currency(self):
        currency
= self.purchase_requisition.currency if self.purchase_requisition else None
       
if currency:
           
return currency.id
       
else:
           
Company = Pool().get('company.company')
            company
= Transaction().context.get('company')
           
if company:
                company
= Company(company)
               
return company.currency.id



 

Sergi Almacellas Abellana

unread,
May 24, 2016, 11:22:21 AM5/24/16
to try...@googlegroups.com, Maxime Richez
You should add purchase_requisition on fields depends, and then it will be called when creating a new record from the One2Many.

Cédric Krier

unread,
May 24, 2016, 11:30:09 AM5/24/16
to tryton
I think if I remember correctly, you must add 'purchase_requisition' in
the depends.

Maxime Richez

unread,
May 25, 2016, 3:24:43 AM5/25/16
to tryton
 

I think if I remember correctly, you must add 'purchase_requisition' in
the depends.
 

Yeah it's working ! :-) Thanks Cedric & Sergi !
Reply all
Reply to author
Forward
0 new messages