Skip to Content
Menu
This question has been flagged

Greetins all,

We are searching for a way to test change on an object just for simulation purpose and not doing it really. For example, we have a stock.move and we want to know if, regarding some constraints on some models fields (stock.quant  for  example) , we would be able to save a certain modification/validate the  stock.move . 

The result should be wether or not the modification is possible. We don't want to really save the modification.

Avatar
Discard
Best Answer

you can use a computed field , let say a boolean that returns true if your validation is ok

Now in @api.depends()  decorator you put your object you want to test and in the compute method  you  put  your  logic.

Exemple:

is_ok=fields.Boolean(compute='test_quant')

@api.depends('stock.quant')

def test_quant(self):

    if \self.stock.quant==your_logic:

        self.is_ok=True

    else:

    self.is_ok=False


Don't put store=True in your compute fields. In this case your function will be called on the fly.

Hope it helps


Avatar
Discard
Author

Hi thanks for your answer.
We have to use constraints validation mechanism with the @api.constrains annotation and validationerror exception here. That is already provided in a another module. That is what we want to "capture" and return true or false if there is exceptions or not. We can't just catch the exception while doing the modification as if there is no exception the modification will be save and we don't want that

Author

We have this code from a community module :

class StockQuant(models.Model):
_inherit = "stock.quant"

@api.constrains("product_id", "quantity")
def check_negative_qty(self):
p = self.env["decimal.precision"].precision_get("Product Unit of Measure")
check_negative_qty = (
config["test_enable"] and self.env.context.get("test_stock_no_negative")
) or not config["test_enable"]
if not check_negative_qty:
return

for quant in self:
disallowed_by_product = (
not quant.product_id.allow_negative_stock
and not quant.product_id.categ_id.allow_negative_stock
)
disallowed_by_location = not quant.location_id.allow_negative_stock
if (
float_compare(quant.quantity, 0, precision_digits=p) == -1
and quant.product_id.type == "product"
and quant.location_id.usage in ["internal", "transit"]
and disallowed_by_product
and disallowed_by_location
):
msg_add = ""
if quant.lot_id:
msg_add = _(" lot '%s'") % quant.lot_id.name_get()[0][1]
raise ValidationError(
_(
"You cannot validate this stock operation because the "
"stock level of the product '%s'%s would become negative "
"(%s) on the stock location '%s' and negative stock is "
"not allowed for this product and/or location."
)
% (
quant.product_id.name,
msg_add,
quant.quantity,
quant.location_id.complete_name,
)
)
The code is called through constraints when new quant or quant modification is requested and raise ValidationError if there is no stock. Want we want to achieve is to be able to check this constraint with the exact same mechanism but we don't want the modification to be done at the end wether or not a ValidationError is raised. For example, we want to test a stock.move validation, it triggers quant creation/modification request, calls the constraints and at the end we need something to "rollback" the action in any case

Related Posts Replies Views Activity
1
May 19
2547
5
Jun 24
1580
1
Feb 24
24
2
Dec 23
1137
0
Dec 22
1346