0

I have the following pydantic models


class Bar(BaseModel):
    foo: int
    
class Foo(BaseModel):
   bar: int
   ba: Bar
   

Is there a way to access the bar attribute from the Foo model when running a validation on the foo attribute of the Bar model? I want to do something like this

 class Bar(BaseModel):
    foo: int
    
    @validator('foo')
    def validate_foo(cls, value, values):
        
        if cls.bar > 1: 
            return 1 
     
        return 0

I've tried accessing the cls.__fields__ attribute but i've found nothing there, is there a better way to do this? Any advice is more than welcome.

Occhima
  • 153
  • 2
  • 9
  • Bar is independent of Foo, so it doesn't know anything about it. You could have a validator in Foo for ba that access bar though. – Simon Hawe Feb 08 '22 at 17:06
  • Maybe you could clarify what your intentions are by doing that validation. – Paul P Feb 08 '22 at 18:00

0 Answers0