Before/after update event


Sorry probably it’s easy to do but it’s still not clear in my head:
When a user update a record and modify one field “Final Quantity” I want to put a boolean of the same model to True. I remember Cedric you told me that it’s not easy to find the field before the write(cls, *args) function and that it’s better to do it after the update. But where ? I’ve tried before super().validate function but it doesn’t work. And if I try after the super().write function it’s also difficult to find the field in *args.

Tks in advance.

You must override the write function and test if your field is in the values key.
This way you will be able to know which records have such field modified and latter do the computation you want.

You can follow the invoice write function as reference. This calls two functions, once before the write call (what you call update) and another after the write call. Feel free to use that as reference to implement your custom needs.

Thanks Sergi, in fact I remember trying this solution but my field is in a One2Many list so it makes the thing more complex…
If I try to catch the modification of this field in his own model, I cannot access the parent model , is it true ?

No, this is not true. You can override write for any model. But you may be also interested on overriding create and delete to catch when the relation is created and deleted.

Let’s say “Pricing” is the model of a One2Many field of model “Position”.
You mean that I can know, thanks to the function Write, if a pricing has been modified in the Position screen via the list of pricing OR directly in the Pricing screen ?
If it’s possible I really want to do it cause it gives a way to know where the user is when he modifies fields !

The write function will be called no mater from which screen the values is updated.
Each time the value is updated your event will be triggered, but you won’t be able to know from which screen the update is done.

Tryton is based on three tiers archictecture, where model and views are independent from each other.

Why do you need to know the screen? Updating the pricing has the same result no mather from which screen you’ve done it.

Ok understood tks Sergi