40 lines
1.2 KiB
Python

Here's the `comments.py` file with Pydantic schemas for comments, following the FastAPI project structure with SQLite and SQLAlchemy:
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field
class CommentBase(BaseModel):
content: str = Field(..., min_length=1, max_length=500)
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
Explanation:
1. We import the necessary modules: `datetime` for working with dates and times, `typing` for type hints, and `pydantic` for defining data models.
5. `CommentInDBBase` inherits from `CommentBase` and adds additional fields for database-related information: `id` (comment ID), `post_id` (ID of the associated post), `created_at` (creation timestamp), and `updated_at` (optional update timestamp). The `Config` class is used to enable the `orm_mode`, which allows Pydantic to work with SQLAlchemy models.