On 2016-06-01 05:04, Maxime Richez wrote:
> Hi,
> 
> I have 2 classes (HEADER and LINES)
> 
> HEADER has a function field currency (because currency need to be stored on 
> the LINES):
> 
> currency = fields.Function(fields.Many2One('currency.currency', 'Default 
> Currency',
>         states=_STATES, depends=_DEPENDS),'on_change_with_currency')
> ...
> 
> @fields.depends('party','company')
>     def on_change_with_currency(self, name=None):
>         cursor = Transaction().connection.cursor()
>         Purchase = Pool().get('purchase.purchase')
>         table = Purchase.__table__()
>         if self.party:
>             subquery = table.select(table.currency,
>                     where=table.party == self.party.id,
>                     order_by=table.id,
>                     limit=10)
>             cursor.execute(*subquery.select(subquery.currency,
>                         group_by=subquery.currency,
>                         order_by=Count(Literal(1)).desc))
>             row = cursor.fetchone()
>             if row:
>                 currency_id, = row
>                 return currency_id
>                 #cls.currency_digits = self.currency.digits
>         elif self.company:
>             return self.company.currency.id
>         return None
> 
> LINES has also a currency field but a many2one (with as default value, 
> currency from HEADER) :
> 
>     currency = fields.Many2One('currency.currency', 'Currency',
>         states={
>             'required': Bool(Eval('unit_price')),
>             },
>         depends=['unit_price'])
> ...
> 
>     @fields.depends('_parent_HEADER.currency','HEADER')
>     def on_change_with_currency(self):
>         currency = self.HEADER.currency if self.HEADER else None
>         if currency:
>             return currency.id
> 
> 
> Currency on the HEADER doesn't need to be displayed, so this field is not 
> in the xml form view. 
> When i try to create a new LINE with a new HEADER, i got this error : 
> 'header' Model has no attribute currency 
> If i had the currency field in the xml form view of the HEADER (visible or 
> not) , there is no error... 
> Why ?

Tryton does not have a mechanism to inject dependencies from One2Many
model to the parent. So it has to be managed manually by adding it to
the view as invisible field.
Otherwise your code fails because self.HEADER is a memory instance which
has only the field the view has.

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

-- 
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/20160601125614.GI23431%40tetsuo.

Reply via email to