Search code examples
odooodoo-14

Show field based on value of another field


I've been trying to show order confirmation date in sale order tree view. To show it I used 'date_order' like this:

<field name="date_order" string="Confirmation Date" optional="show" attrs="{'invisible':[['state','not in',['sale', 'done']]]}" />

In our setup orders are created manually but also synced from Woocommerce and in those synced orders from web shop date_order is always before the create date.

For example order is created (create_date) 10.08.2022 17:10:20 and confirmed automatically (date_order) 10.08.2022 17:10:11

Somehow, it is confirmed 9s before it is created. Now, i'd like to show date_order only when it's value is > create_date. In other cases i'd like to display create_date in it's place.

Tryed to use attrs for this, I'm not sure if it's even possible:

<field name="date_order" string="Confirmation Date" optional="show" attrs="{'invisible':['&', ['state','not in',['sale', 'done']], ['date_order'], '>', ['create_date']]}" />

The code above gives XMLSyntaxError. Not sure if it's possible to compare values in that manner - and finally how to get one or the other value - I guess my second approach is maybe better.

In second approach I tried to create compute field, like this:

date_order_mine = fields.Char("Potvrdjeeeno", compute="comp_date")
@api.depends('create_date', 'date_order', 'order_line')       
def comp_date(self):
    for order in self:
        for line in order.order_line:
            if line.create_date < line.date_order:
                return line.date_order
            else:
                return line.create_date

This code gives me AttributeError: 'sale.order.line' object has no attribute 'date_order'

Since I'm so new to Odoo and Python dev I'm not sure what should I do here to compare values of this fields and to return one or other based on conditions - if someone can help I will appreciate it.


Solution

  • The domain used in attrs is not valid, domains should be a list of criteria, each criterion being a triple (either a list or a tuple) of: (field_name, operator, value)

    In your second approach, the date_order field is on the parent model sale.order (order) and to access the date order from order lines , use the order_id field like following:

    line.order_id.date_order
    

    To set the value of date_order_mine to create_date or date_order you do not need the order lines and also the computed method should assign the compute value:

    Computed Fields
    Fields can be computed (instead of read straight from the database) using the compute parameter. It must assign the computed value to the field.

    Example:

    date_order_mine = fields.Datetime("Potvrdjeeeno", compute="comp_date")
    
    @api.depends('create_date', 'date_order')       
    def comp_date(self):
        for order in self:
            if order.create_date < order.date_order:
                order.date_order_mine = order.date_order
            else:
                order.date_order_mine = order.create_date