How to use values from list to validate some calculations in pydantic BaseModel?

Tags: ,

I’m using Pydantic root_validator to perform some calculations in my model:

class ProductLne(BaseModel):
    qtt_line: float = 0.00
    prix_unite: float = 0.00
    so_total_ht: float = 0.00
    class Config:
        validate_assignment = True

    def calculat_so_totals(cls, values):
      values["so_total_ht"] = values.get("qtt_line")*values.get("prix_unite")
    return values

class Bon(BaseModel):
    articles: List[ProductLne] = []
    total_ht: float = 0.00

    class Config:
        validate_assignment = True

    def set_total_ht(cls, values):
        for item in values.get('articles'):
            values['total_ht'] += item.so_total_ht
        return values

some data

 item_line1 = ProductLne(qtt_line=10, prix_unite=10.00)
 item_line2 = ProductLne(qtt_line=10, prix_unite=12.00)
 bon1 = Bon()

when run


i get : 0.0, O.OO Iwant 220

How to make this function return the correct values?


I do not know if this is good why , but i get what i want

from pydantic import BaseModel, root_validator, Field
from typing import List
from typing import TYPE_CHECKING, Union
    from pydantic.typing import DictStrAny

class PropertyBaseModel(BaseModel):

def get_properties(cls):
    return [
        prop for prop in dir(cls)
        if isinstance(getattr(cls, prop), property) and prop not in ("__values__", "fields")

def dict(self, *args, **kwargs) -> 'DictStrAny':
    self.__dict__.update({prop: getattr(self, prop) for prop in self.get_properties()})

    return super().dict(*args, **kwargs)

class ProductLne(PropertyBaseModel):
    prix: float = 0.00
    qtt_line: float = 0.0

    def so_total_ht(self) -> float:
        return self.qtt_line * self.prix

class Bon(BaseModel):
    articles: List[ProductLne] = []

    def total_ht(self) -> float:
        bla = 0.00
        for item in self.articles:
            bla += item.so_total_ht
    return bla

item_line1 = ProductLne(prix=10.00,qtt_line=10)
item_line2 = ProductLne(prix=12.00,qtt_line=10)


bon1 = Bon()
print(bon1.total_ht) #220

Source: stackoverflow