I tried some stuffs but without success In my project I have:
- a model
Hospital
and a modelUser
(I keep in distinct files) - a schema
Hospital
and shemaUser
(I keep in distinct files too)
The relationship is one to one.
I need add to my response model Hospital
a boolean field from User
(the field is accept_terms
)
So I created another schema in User
file called UserAcceptTerms
that inherit from User
but when I run it show me a error on terminal asking for fields required from User
schema.
In another way for solution I put my subclass to inherit from BaseModel
and I set for a boolean field a defalut value = False
. It's run but the value is wrong for my test that I hope a True
value like response.
My Code:
#Schema for Hospital
class HospitalBase(BaseModel):
code: str
scode: str
name: str
type: str
email1: str = None
class Hospital(HospitalBase):
id: int
created_at: datetime
updated_at: datetime
user_id: int #foreignkey from User
deleted: bool = False
first_access: bool = False
class Config:
orm_mode = True
class HospitalUser(UserAcceptTerms,Hospital): #Here my schema that I'll use like a response_model
pass
class Config:
orm_mode = True
#Schema for User
class UserBase(BaseModel):
name: str
email: str = None
login: str = None
is_superuser: bool = False
is_staff: bool = False
class User(UserBase):
id: int
created_at: datetime
updated_at: datetime
is_superuser: bool
is_staff: bool
deleted: bool = False
accept_terms: bool = False
class Config:
orm_mode = True
#Here using User schema --> problem with required field
class UserAcceptTerms(User):
accept_terms: bool = False
class config:
orm_mode = True
#Here using BaseModel --> problem don't show a corret value from Database
class UserAcceptTerms(BaseModel):
accept_terms: bool = False
class config:
orm_mode = True
Response body
[
{
"code": "3",
"scode": "XXX0003",
"name": "Hospital XXX",
"type": "H",
"email1": "test@test.com",
"id": 1,
"created_at": "2021-12-22T14:28:58.670072",
"updated_at": "2022-08-30T13:06:40.752472",
"user_id": 4,
"deleted": false,
"first_access": true,
"accept_terms": false #This value is wrong because is TRUE in database
},
Does someone have any idea for help me? Thanks :)