Para tener un mayor orden en el codigo y teniendo en cuenta el concepto de paquetes, lo mas optimo es crear los modelos en un archivo aparte puede llamarse models y ser importado a nuestro archivo main
Para el caso de uso actual quedaria algo asi.
models
# Python
from uuid import UUID
from datetime import date, datetime
from typing import Optional
# Pydantic
from pydantic import BaseModel
from pydantic import Field, EmailStr
class User(BaseModel):
user_id: UUID = Field(..., alias="id")
email: EmailStr = Field(..., example="[email protected]")
first_name: str = Field(..., min_length=2, max_length=50, example="John")
last_name: str = Field(..., min_length=2, max_length=50, example="Doe")
birthday: Optional[date] = Field(default=None)
class UserIn(User):
password: str = Field(..., min_length=8, max_length=30)
class UserOut(User):
pass
class Tweet(BaseModel):
tweet_id: UUID = Field(..., alias="Tweet id")
content: str = Field(..., min_length=2, max_length=240, example="Hello World")
created_at: datetime = Field(default=datetime.now())
updated_at: Optional[datetime] = Field(default=None)
by: UserIn = Field(..., alias="User")
main
# Python
# Pydantic
# FastAPI
from fastapi import FastAPI
# Models
from models import User, UserIn, UserOut
from models import Tweet
app = FastAPI()
@app.get(path="/home", tags=["Home"])
def home():
return {"message": "Welcome"}
Los aportes, preguntas y respuestas son vitales para aprender en comunidad. Regístrate o inicia sesión para participar.