‘from_orm’ is a class method, and is used to get the model from ORM instance.
Step 1: Define a model and set the orm_mode property to true.
class EmployeeModel(BaseModel):
id: int
name: constr(max_length=63)
age: int
class Config:
orm_mode = True
Step 2: Use the model class method ‘from_orm’ to get the model from orm instance.
empModel = EmployeeModel.from_orm(empOrm)
Find the below working application.
from_orm_1.py
from typing import List
from sqlalchemy import Column, Integer, String
from sqlalchemy.dialects.postgresql import ARRAY
from sqlalchemy.ext.declarative import declarative_base
from pydantic import BaseModel, constr
Base = declarative_base()
class EmployeeOrm(Base):
__tablename__ = 'employees'
id = Column(Integer, primary_key=True, nullable=False)
name = Column(String(63), unique=True)
age = Column(Integer, nullable=False)
class EmployeeModel(BaseModel):
id: int
name: constr(max_length=63)
age: int
class Config:
orm_mode = True
empOrm = EmployeeOrm(
id=123,
name='Krishna',
age=31
)
print('empOrm -> ' + str(empOrm))
empModel = EmployeeModel.from_orm(empOrm)
print('empModel -> ' + str(empModel))
Output
empOrm -> <__main__.EmployeeOrm object at 0x1083af670> empModel -> id=123 name='Krishna' age=31
No comments:
Post a Comment