39 lines
1.0 KiB
Python

Here's the `posts.py` file with Pydantic schemas for posts, located in the `app/api/v1/schemas/` directory:
from typing import Optional
from pydantic import BaseModel
from datetime import datetime
class PostBase(BaseModel):
title: str
content: str
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 PostList(BaseModel):
__root__: list[Post]
Explanation:
1. We import the necessary modules: `typing` for type annotations, `pydantic` for defining data models, and `datetime` for working with date and time objects.
2. `PostBase` is a Pydantic model that defines the base fields for a post: `title` and `content`.
5. `PostInDBBase` inherits from `PostBase` and adds additional fields: `id`, `created_at`, and `updated_at`. The `orm_mode` config is set to `True` to allow Pydantic to read data from SQLAlchemy models.