Pydantic
- Refer Here for pydantic
- This is popular library for handling data models with validations
Pydantic Tutorial
- Install pydantic package
uv add pydantic
- Create a notebook
.ipynb
- Ensure python and Jupyter extensions from microsoft are installed into vscode
- Refer Here for validators
- Prompt for learning pydantic
You are an expert python programmer, I'm learning and today i want to try pydantic. Give me examples to use existing and custom validations in both v1 and v2 . Give me one example wait for me to execute then go to next
- Exercise: try to comeup with Order Model
{
"orderId": "ORD_64829",
"customerId": "CUST_144644",
"customerName": "Isha Kapoor",
"orderDate": "2025-12-08T02:32:07.348249",
"status": "delivered",
"restaurant": {
"name": "Domino's Pizza",
"restaurantId": "REST_99294",
"location": "Gachibowli, Hyderabad",
"rating": 4.5,
"deliveryTime": "23 mins"
},
"items": [
{
"name": "Choco Lava Cake",
"quantity": 1,
"price": 89.0,
"customizations": []
}
],
"pricing": {
"itemTotal": 89.0,
"deliveryFee": 0.0,
"platformFee": 5.0,
"gst": 4.45,
"discount": 8.9,
"totalAmount": 89.55
},
"payment": {
"method": "UPI",
"transactionId": "TXN_86705151",
"status": "paid"
},
"deliveryAddress": {
"label": "Home",
"address": "Flat 402, Green Valley Apartments, Road No 12, Banjara Hills",
"city": "Hyderabad",
"pincode": "500034"
},
"deliveryPartner": {
"name": "Sanjay Reddy",
"rating": 4.9,
"phone": "+91-0803842694"
},
"timeline": {
"orderPlaced": "2025-12-08T02:32:07.348249",
"restaurantAccepted": "2025-12-08T02:32:07.348249",
"foodReady": "2025-12-08T02:32:07.348249",
"outForDelivery": "2025-12-08T02:32:07.348249",
"delivered": "2025-12-08T02:32:07.348249"
},
"ratings": {
"food": 5,
"delivery": 4,
"review": "Pretty good!"
}
}