1

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.

  • 1
    Such domains `field_a is less field_b` and similar ones aren't possible. Alternatively you can create a computed field, which implements your logic and then can be used in the list view. – CZoellner Aug 16 '22 at 13:39
  • Yeah, I thought so - I tried to create computational field but got stuck. Answer from Kenly helped me. – Matija Amondi Aug 17 '22 at 06:43

1 Answers1

2

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
Kenly
  • 24,317
  • 7
  • 44
  • 60
  • 1
    Great explanation once again Kenly - thank you so much. I knew this computational field was close. This is working solution, and as you mentioned results are Datetime type. – Matija Amondi Aug 17 '22 at 06:47
  • 1
    @Kenly wouldn't it be a better answer without that "note" and instead changing the `fields.Char` in the code example to `fields.Datetime`? Except from that, good answer. Oh and the computed field has to be stored, if somebody wants to use it for ordering or searching (and grouping > datetime better again, too). – CZoellner Aug 17 '22 at 07:42
  • @CZoellner Yes of course, I wanted him to notice the difference. Thanks. – Kenly Aug 17 '22 at 12:11