37 lines
1.0 KiB
Python

Here's the `posts.py` file with Pydantic schemas for posts, to be placed in the `app/api/v1/schemas/` directory:
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field
class PostBase(BaseModel):
title: str = Field(..., min_length=3, max_length=100)
content: str = Field(..., min_length=10)
class PostCreate(PostBase):
pass
class PostUpdate(PostBase):
pass
class PostInDBBase(PostBase):
id: int
created_at: datetime
updated_at: Optional[datetime] = None
class Config:
orm_mode = True
class Post(PostInDBBase):
pass
class PostInDB(PostInDBBase):
pass
This file defines the following Pydantic models:
These models can be used in the FastAPI routes and services to validate and handle the data related to posts. They ensure data consistency and provide a clear separation between the different operations (create, update, read) on posts.
Note: Make sure to import the necessary dependencies (`datetime`, `typing`, and `pydantic`) at the top of the file.