Error : Model has no attribute ... when function field is not in my xml form view (visible or not)

31 views
Skip to first unread message

Maxime Richez

unread,
Jun 1, 2016, 8:04:16 AM6/1/16
to tryton
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 ?

Hope my explanations are understandable :-)





Cédric Krier

unread,
Jun 1, 2016, 9:00:03 AM6/1/16
to tryton
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: cedric...@b2ck.com
Tel: +32 472 54 46 59
Website: http://www.b2ck.com/

Maxime Richez

unread,
Jun 1, 2016, 9:02:53 AM6/1/16
to tryton

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.


Ok!  Thanks for the explanations :-)
Reply all
Reply to author
Forward
0 new messages