38 lines
1.1 KiB
Python
38 lines
1.1 KiB
Python
Here's the `comments.py` file with Pydantic schemas for comments, to be placed in the `app/api/v1/schemas/` directory:
|
|
|
|
from typing import Optional
|
|
from pydantic import BaseModel
|
|
from datetime import datetime
|
|
|
|
class CommentBase(BaseModel):
|
|
content: str
|
|
|
|
class CommentCreate(CommentBase):
|
|
pass
|
|
|
|
class CommentUpdate(CommentBase):
|
|
pass
|
|
|
|
class CommentInDBBase(CommentBase):
|
|
id: int
|
|
post_id: int
|
|
created_at: datetime
|
|
updated_at: Optional[datetime] = None
|
|
|
|
class Config:
|
|
orm_mode = True
|
|
|
|
class Comment(CommentInDBBase):
|
|
pass
|
|
|
|
class CommentInDB(CommentInDBBase):
|
|
pass
|
|
|
|
Here's a breakdown of the file:
|
|
|
|
1. We import the necessary modules: `typing` for type hints, `pydantic` for defining data models, and `datetime` for working with date and time objects.
|
|
|
|
|
|
|
|
|
|
5. `CommentInDBBase` inherits from `CommentBase` and adds fields for the comment's ID (`id`), the associated post ID (`post_id`), the creation timestamp (`created_at`), and an optional update timestamp (`updated_at`). The `Config` class is used to enable the `orm_mode` for compatibility with SQLAlchemy models. |