I have this sqlmodel:
class User(SQLModel, table=True):
__tablename__ = "users"
user_id: Optional[int] = Field(primary_key=True, default=None)
username: str = Field(unique=True)
role_id: int = Field(foreign_key='roles.role_id')
role: Role = Relationship(back_populates="users")
in the fastapi payload I receive role_description which is a string so I have to find the id associated to that description;
Is it possible to do something like this:
class User(SQLModel, table=True):
__tablename__ = "users"
user_id: Optional[int] = Field(primary_key=True, default=None)
username: str = Field(unique=True)
role_id: int = Field(foreign_key='roles.role_id')
role: Role = Relationship(back_populates="users")
@validator('role_id')
def from_role_descr_to_role_id(self, role_descr: str, role:Role):
return role.role_id
If it is not, how can I avoid making the select query?