31 lines
924 B
Python
31 lines
924 B
Python
Here's the `posts.py` file with Pydantic schemas for posts, which can be placed in the `app/api/v1/schemas/` directory:
|
|
|
|
from typing import Optional
|
|
from pydantic import BaseModel
|
|
from datetime import datetime
|
|
|
|
class PostCreate(BaseModel):
|
|
title: str
|
|
content: str
|
|
|
|
class PostRead(BaseModel):
|
|
id: int
|
|
title: str
|
|
content: str
|
|
created_at: datetime
|
|
updated_at: Optional[datetime] = None
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
class PostUpdate(BaseModel):
|
|
title: Optional[str] = None
|
|
content: Optional[str] = None
|
|
|
|
Explanation:
|
|
|
|
1. `PostCreate` is a Pydantic model that defines the required fields (`title` and `content`) for creating a new post.
|
|
3. `PostUpdate` is a Pydantic model that defines the optional fields (`title` and `content`) for updating an existing post.
|
|
|
|
|
|
Note: Make sure to import the necessary dependencies (`typing`, `pydantic`, and `datetime`) at the top of the file. |