38 lines
1.0 KiB
Python
38 lines
1.0 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
|
|
|
|
from app.db.models import Comment
|
|
|
|
class CommentBase(BaseModel):
|
|
body: str
|
|
|
|
class CommentCreate(CommentBase):
|
|
post_id: int
|
|
|
|
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:
|
|
|
|
2. `CommentBase` is a Pydantic model representing the base fields of a comment (body).
|
|
5. `CommentInDBBase` inherits from `CommentBase` and includes additional fields (`id`, `post_id`, `created_at`, and `updated_at`) that are typically stored in the database. The `orm_mode` config is set to `True` to allow reading data from an ORM model. |