프로그래밍/Python

pydantic 버전

이재만박사 2025. 5. 2. 21:24

Pydantic v1 dict() 사용

 

Pydantic v2 model_dump() 사용

 

버전 상관없이 호환성 유지 원할 때

 

dict()는 파이썬 내장 메서드(__dict__)와 겹쳐서 혼란을 줄 수 있음

 

model_dump()는 Pydantic 모델에 특화된 메서드라는 점이 더 분명

 

V2에서는 model_dump(), model_validate(), model_construct() 등 model_ 접두어를 붙여

명확성과 예측 가능성을 강화

 

**dict는 파이썬의 언패킹 연산자

 

딕셔너리의 키-값 쌍을 함수나 메서드에 **키워드 인수로 전달하는 문법

 

data = {'title': 'apple', 'content': 'Apple Tree' }

 

Post.objects.create(**data)

이것은 아래와 동일함

 

Post.objects.create(title='apple', content='Apple Tree')