Reputation: 49
I am using FastAPI and SQLAlchemy. I have two ORM models users and user_profiles with foreign key relation. I am inserting data to user and user_profiles using one model
#1st model.
class User():
__tablename__: str = "user"
#2 Model
class UserProfile:
__tablename__: str = "user_profiles"
my user request model to update the data:
class UserRequest():
user_id:str=someval()
My question is how I can include user_profiles model in UserRequest and update the values.
Upvotes: 0
Views: 197
Reputation: 523
If by UserRequest
you mean pydantic model, then it can be done something like this
DB Models
class User(sql.Model):
a: str = Field()
b: str = Field()
properties: UserProperties = ForeignKey()
class UserProperties(sql.Model):
c: str = Field()
d: str = Field()
Pydantic models
class UserDTO(pydantic.BaseModel)
a: str
b: str
class UserPropertiesDTO(pydantic.BaseModel)
c: str
d: str
Then
def request(
a: str,
b: str,
c: str,
d: str,
) -> Response:
User.objects.create(
**UserDTO(a, b).dict(),
properties=UserPropertiesDTO(c, d),
)
return SuccessfulResponse
Upvotes: 0