4/app/api/v1/schemas/comments.py
2025-03-20 09:54:31 +01:00

41 lines
1.6 KiB
Python

Ensure all necessary imports and dependencies are included, maintaining consistency with a FastAPI project structure using SQLite and SQLAlchemy.
Here's the 'comments.py' file with the requested schemas:
```python
# app/api/v1/schemas/comments.py
from typing import Optional
from pydantic import BaseModel
from datetime import datetime
# CommentsCreate schema for creating a new comment
class CommentsCreate(BaseModel):
content: str
post_id: int
user_id: int
# Comments schema for reading/returning comments
class Comments(BaseModel):
id: int
content: str
created_at: datetime
updated_at: Optional[datetime] = None
post_id: int
user_id: int
class Config:
orm_mode = True
```
Explanation:
1. The necessary imports are included: `typing` for type hints, `pydantic` for defining the schemas, and `datetime` for handling date and time fields.
2. The `CommentsCreate` schema is defined with fields `content` (str), `post_id` (int), and `user_id` (int). This schema will be used for creating new comments.
3. The `Comments` schema is defined with fields `id` (int), `content` (str), `created_at` (datetime), `updated_at` (Optional[datetime]), `post_id` (int), and `user_id` (int). This schema will be used for reading and returning comments.
4. The `orm_mode = True` configuration is set in the `Comments` schema, which tells Pydantic to read data from SQLAlchemy ORM models.
Note: This file assumes that the `comments` table in the database has columns corresponding to the defined fields in the schemas. If the table structure is different, the schemas should be adjusted accordingly.