Comments (3)
class User(UserBase):
id: int
is_active: bool
items: List[Item] = []
class Config:
orm_mode = True
- pydantic model에 Config 클래스 안에 orm_mode를 True로 설정한다.
- 이를 통해서 pydantic model이 orm model과 compatible하다.
from til.
CRUD 예시
from sqlalchemy.orm import Session
from . import models, schemas
def get_user(db: Session, user_id: int):
return db.query(models.User).filter(models.User.id == user_id).first()
def get_user_by_email(db: Session, email: str):
return db.query(models.User).filter(models.User.email == email).first()
def get_users(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.User).offset(skip).limit(limit).all()
def create_user(db: Session, user: schemas.UserCreate):
fake_hashed_password = user.password + "notreallyhashed"
db_user = models.User(email=user.email, hashed_password=fake_hashed_password)
db.add(db_user)
db.commit()
db.refresh(db_user)
return db_user
def get_items(db: Session, skip: int = 0, limit: int = 100):
return db.query(models.Item).offset(skip).limit(limit).all()
def create_user_item(db: Session, item: schemas.ItemCreate, user_id: int):
db_item = models.Item(**item.dict(), owner_id=user_id)
db.add(db_item)
db.commit()
db.refresh(db_item)
return db_item
- 들어오는 데이터의 타입 검사는 pydantic이 수행
from til.
from .database import SessionLocal, engine
models.Base.metadata.create_all(bind=engine)
- main.py에서는 이렇게 engine과 model을 import 해주어서 사용한다.
from til.
Related Issues (20)
- kafka topic and partition HOT 4
- kafka producer detail HOT 2
- kafka consumer detail HOT 4
- kafka toy project HOT 1
- kafka 사용 인프라 아키텍쳐 HOT 1
- log collectors HOT 1
- ML monitoring - Intro HOT 1
- ML system testing concepts HOT 4
- ML model prediction quality test
- ML model configuration test
- DS school - Unit testing production model HOT 1
- DS School - Config Test HOT 1
- DS School - Differential test
- DS School - Shadow mode HOT 1
- DS School - ML system metrics
- elasticsearch - shard, segment HOT 1
- torchscript HOT 1
- torch-model-archiver
- python grpc HOT 1
- asyncio - coroutine HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from til.