One2Many with virtual Entries in SaleLine

37 views
Skip to first unread message

Hendrik Brandes

unread,
Feb 25, 2016, 3:50:04 AM2/25/16
to tryton-dev
Hello, 

I have the following use case: We have a module called "sale_discount", which enables discounting rules ( a table ) for different quantities of a product.

First I have implemented a wizard like TestPaymentTerm, to display the different values. Works nice. 
Second, I want to display this as a separate page in the sale_line_form, so the user can see each condition of the product.

In SaleLine, I want to define the pricing_info as following:

    pricing_info = fields.One2Many('ebb.product.pricing.test.result',
       
None, 'Result', readonly=True)

The method on_change_with_pricing_info returns the values and works also:
    @fields.depends('product', 'unit', 'pricing_info')
   
def on_change_with_pricing_info(self):
...        
       
self.result = result
       
return self._changed_values.get('result', [])

but when I execute this in the client on a sale I get the error

...
 
File "/trytond/model/fields/one2many.py", line 84, in get
    field
= Relation._fields[self.field]
KeyError: None
...


I understand, that the One2Many needs a reference to the parent, but why does this works in the TestPaymentTerm Dialog?
I guess, this might be the fact, that in the dialog is no reference to something else and only the tree/list of the PaymentTermResult is displayed.

What will be a good way to implement such a feature in the sale_line? Does it need, to create a ModelSQL? 
Currently, the class "result" is implemented as ModelView with the needed fields as readonly.

I do not want to store every discount for each product within the database.

Thank you and best regards,
Hendrik

Cédric Krier

unread,
Feb 25, 2016, 4:30:03 AM2/25/16
to tryton-dev
On 2016-02-24 21:34, 'Hendrik Brandes' via tryton-dev wrote:
> Hello,
>
> I have the following use case: We have a module called "sale_discount",
> which enables discounting rules ( a table ) for different quantities of a
> product.

This sounds like the price list.

> First I have implemented a wizard like TestPaymentTerm, to display the
> different values. Works nice.
> Second, I want to display this as a separate page in the sale_line_form, so
> the user can see each condition of the product.
>
> In SaleLine, I want to define the pricing_info as following:
>
> pricing_info = fields.One2Many('ebb.product.pricing.test.result',
> None, 'Result', readonly=True)
>
> The method on_change_with_pricing_info returns the values and works also:
> @fields.depends('product', 'unit', 'pricing_info')
> def on_change_with_pricing_info(self):
> ...
> self.result = result
> return self._changed_values.get('result', [])
>
> but when I execute this in the client on a sale I get the error
>
> ...
> File "/trytond/model/fields/one2many.py", line 84, in get
> field = Relation._fields[self.field]
> KeyError: None
> ...
>
>
> I understand, that the One2Many needs a reference to the parent, but why
> does this works in the TestPaymentTerm Dialog?

Because wizard doesn't use ModelStorage but only ModelView and so it
never tries to save or read it from the database.

Maybe we could add a standard test to detect such case when the relation
field are missing on ModelStorage class.

> What will be a good way to implement such a feature in the sale_line? Does
> it need, to create a ModelSQL?
> Currently, the class "result" is implemented as ModelView with the needed
> fields as readonly.

Clearly, this is information that you compute so you must use a Function
field. Or if the list can be based on a SQL query, you can define a
ModelSQL as relation which is based on a 'table_query' [1] query.

[1] http://doc.tryton.org/3.8/trytond/doc/ref/models/models.html?#trytond.model.ModelSQL.table_query
--
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